【发布时间】:2012-02-06 23:24:14
【问题描述】:
所以,我有下面表示的 4 个实体,它们在我的应用程序中是强大且独立的实体,现在的问题是每个文章或图片都可以用演示者或事件“标记”,因为它们是 4 个它们是可能变得更复杂的独立实体 将 Event 和 Presenter 字段添加到 Article 和 Picture 或相反看起来不正确,特别是因为它们可能被标记为 none。
从长远来看,可能还需要标记其他实体,并且可能会出现其他可标记实体。
class Article(models.Model):
#Fields
class Picture(models.Model):
#Fields
class Presenter(models.Model):
# Fields
class Event(models.Model):
# Fields
我越来越接近像这样的某种基于通用内容类型的双头中间模型(尚未测试,因为它比那更复杂一点),但我正在寻找想法:
class GenericTag(models.Model):
# Event,Presenter instance..
tagcontent_type = models.ForeignKey(ContentType)
tagobject_id = models.PositiveIntegerField()
tagcontent_object = generic.GenericForeignKey('tagcontent_type', 'tagobject_id')
# Picture,Article instance
objcontent_type = models.ForeignKey(ContentType)
objobject_id = models.PositiveIntegerField()
objcontent_object = generic.GenericForeignKey('objcontent_type', 'objobject_id')
然后根据我所拥有的信息进行查询,我认为必须有更优雅的方法来做到这一点,而不会将所有标签模型作为字段填充到可标记模型中。
【问题讨论】:
-
是多对多关系还是一对多关系?从您的问题来看,文章或图片可能只有一个演示者和一个事件,但标题显示“many2many”。
-
一张图片可以有多个演示者,一个演示者可以在多张图片中,文章和活动也是如此。
标签: python django django-models django-contenttypes