Django中复杂关系的建模

时间:2013-02-18 22:46:41

标签: django orm many-to-many data-modeling django-orm

好的,这是一个挑战,Djangonistas!

我想说我的项目有 3 类型的数据。

  • 项目
  • 音频
  • 视频

项目可以引用无限数量的视频和音频对象,以及对其他项目的无限引用。

对此进行建模的最佳方法是什么?它必须以通用的方式完成,以便将来可以添加新的对象类型(图像,链接,气味等),并且可以由多个项目引用对象。

我有3个关于如何做到这一点的理论。

#1:继承

创建一个BaseModel类,让所有其他对象继承它。

class BaseModel(models.Model)
class Project(BaseModel)
class Video(BaseModel)
etc

然后,将一个ManyToMany字段添加到Project类中的所有BaseModel。

class Project(BaseModel):
    content = models.ManyToManyField(BaseModel)

这会起作用吗?

#2:通用外键

我还可以设置系统,该系统将使用可以使用ContentTypes框架的GenericForeignKey字段

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')

(我认为这可能是最糟糕的主意。)

#3:Generic ManytoMany Keys

最后,我可以有一个单独的表来维护对象之间的所有关系。

class RelatedObject(models.Model):
    """
    A generic many-to-many implementation where diverse objects are related
    across a single model to other diverse objects -> using a dual GFK
    """
    # SOURCE OBJECT:
    parent_type = models.ForeignKey(ContentType, related_name="child_%(class)s")
    parent_id = models.IntegerField(db_index=True)
    parent = GenericForeignKey(ct_field="parent_type", fk_field="parent_id")

    # ACTUAL RELATED OBJECT:
    object_type = models.ForeignKey(ContentType, related_name="related_%(class)s")
    object_id = models.IntegerField(db_index=True)
    object = GenericForeignKey(ct_field="object_type", fk_field="object_id")

    alias = models.CharField(max_length=255, blank=True)
    creation_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('-creation_date',)

    def __unicode__(self):
        return '%s related to %s ("%s")' % (self.parent, self.object, self.alias)

然后

class Project(models.Model):
    related = RelatedObjectsDescriptor()

Charles Leifer在此更详细地阐述了这一点:http://charlesleifer.com/blog/connecting-anything-to-anything-with-django/和他的django-generic-m2m项目:https://github.com/coleifer/django-generic-m2m

怎么办?

那么,这些存储关系的方法有哪些优缺点?哪个是最快的,哪个更容易编写和维护?你过去使用过什么方法?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我会说你想要'self'作为Project模型'ManyToManyField的参数(而不是你示例中继承的东西)。这允许您定义自引用模型。

如果你看https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey - 你会看到两行下来,就有这样的说明:

要创建递归关系 - 与自身具有多对一关系的对象 - 请使用models.ForeignKey('self')。

...我从2008年开始从这篇博客文章中了解到这一点(现在可能已经过时了,但在使用这种技术时我仍然会参考它):

http://eflorenzano.com/blog/2008/05/17/exploring-mixins-django-model-inheritance/

确实,祝你好运先生。

相关问题