【发布时间】:2010-08-07 11:19:43
【问题描述】:
在我的应用程序中,我的模型定义如下:
class SomeModel(models.Model):
someFK = Models.ForeignKey(SomeModel, null=True, blank=True)
otherFK = Models.ForeignKey(OtherModel, null=True, blank=True)
...
此模型保存有关日志的数据,并且要记录的模型不止一种,因此我为每个相关模型放置了一个 FK。对于每条记录,只使用一个FK,其他FK设置NULL = True。
但是当我需要记录其他东西时,我不想改变我的模型。所以我改变了我的模型:
class SomeModel(models.Model):
content_type = models.ForeignKey(ContentType)
content_id = models.IntegerField()
所以我用以下来得到我想要的:
content_type.get_object_for_this_type(id=content_id)
没关系。但是当 admin.py 发挥作用时,它会导致问题,因为我有 1.000.000+ 数据用于记录一些相关模型,所以我需要使用 raw_id_fields。由于我没有为每个相关模型提供 FK,因此我必须使用 ContentType 模型和 content_id,但我不知道如何使用 ContentType?
【问题讨论】: