首页 > 解决方案 > 如何为 django 简单历史表设置 manage=False

问题描述

我正在使用django-simple-history我的模型。我正在删除模型上的一个字段,但将meta属性设置managedFalse. 然而,这并不能转化为简单的历史表。有没有办法做到这一点?

标签: pythondjangodjango-modelsdjango-simple-history

解决方案


您可以子类化HistoricalRecords该类,并覆盖该get_meta_options函数。例如:

from simple_history.models import HistoricalRecords

class UnmanagedHistoricalRecords(HistoricalRecords):

    def get_meta_options(self, model):
        result = super().get_meta_options(model)
        result['managed'] = False
        return result

然后我们可以UnmanagedHistoricalRecords在我们的模型中使用我们的类:

class SomeModel(models.Model):
    # …

    objects = UnmanagedHistoricalRecords()

因此,我们将在此处将 a 传递managed = False给将由UnmanagedHistoricalRecords对象管理器构造的模型。


推荐阅读