在Django中归档模型数据 [英] Archiving model data in Django
问题描述
我正在使用一个超过我预期的django应用程序,现在需要归档一些数据,因此不会减慢最终的查询。我的问题是,归档现有模型的最佳方式是什么?这是我以前想到的:
1 - 创建一个继承的模型:
code> class OriginalModel(models.Model):
...
field = models.CharField(etc ...)
...
Class ArchivedModel(OriginalModel):
pass
2 - 创建一个post_save方法,保存到我的原始模型中,它将创建一个归档的实例。
3 - 在原始模型上创建一个清除任务与芹菜,只保留相关数据。 p>
我不确定然而,如果模型继承可以按照我的期望工作。如果有人对此有一些意见,我将非常感谢。
在的代码数据中,存档模型
将保留在 OriginalModel
SQL表中。在SQL级别 ArchivedModel
将是一个单个 id
字段引用到 OriginalModel
表。因此,如果您要在 post_save
信号中的 ArchivedModel
中添加对象,它们将在 OriginalModel
: - )
您需要创建抽象基类,并继承其中的真实模型:
class AbstractModel(models.Model):
...
field = models.CharField(etc ...)
...
class Meta :
abstract = True
class OriginalModel(AbstractModel):
pass
class ArchivedModel(AbstractModel):
pass
在这种情况下 OriginalModel
和 ArchivedModel
将具有不同的SQL表与同一组字段。
I am working with a django app that grew more than I expected and now need to archive some data so it doesn't slow down eventual queries. My question is, what is the best way to archive an existing model? Here is what I have thought so far:
1 - Create an inherited model:
class OriginalModel(models.Model):
...
field = models.CharField(etc...)
...
class ArchivedModel(OriginalModel):
pass
2 - Create a post_save method so whenever something is saved to my original model it will create an archived instance as well.
3 - Create a purge task with celery on my original model to keep only the relevant data.
I am not sure however if model inheritance will work as I expect it though. If anyone has some input on this I will greatly appreciate it.
In your code data from ArchivedModel
will stay in OriginalModel
SQL table. At SQL level ArchivedModel
will be a table with single id
field referencing to OriginalModel
table. So if you will add objects to ArchivedModel
in post_save
signal they will be duplicated in OriginalModel
:-)
You need to create abstract base class and inherit both "real" models from it:
class AbstractModel(models.Model):
...
field = models.CharField(etc...)
...
class Meta:
abstract = True
class OriginalModel(AbstractModel):
pass
class ArchivedModel(AbstractModel):
pass
In this case OriginalModel
and ArchivedModel
will have different SQL tables with the same set of fields.
这篇关于在Django中归档模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!