Django多对多,具有模型继承

时间:2022-09-02 20:15:00

I'm looking to use Django's Generic models in a ManyToMany style relationship where one model inherits from a base class, and the other model doesn't inherit can have multiple references to many of those inherited models, and those inherited models can also belong to many different instances of that class (e.g. in this case Author). Example of the structure of my models is

我想使用Django的通用模型在多风格的关系,一个模型继承自基类,和其他模型没有继承可以有多个引用许多遗传模型,和那些继承模型也可以属于不同的类的实例(例如,在这种情况下,作者)。我的模型结构的例子是

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType

class BaseModel(models.Model):
    class Meta:
        abstract = True

class Book(BaseModel):
    pass

class Article(BaseModel):
    pass

Among other additional models that inherit from the BaseModel, then I have

在其他继承自BaseModel的模型中,我有

class Author(models.Model):
    object_id = models.PositiveIntegerField()
    content_type = models.ForeignKey(ContentType)
    publications = generic.GenericForeignKey('content_type', 'object_id')

However, this doesn't seem to a allow me to add multiple publications to the Author model. In this case, all the publication sources can have many authors and authors can have many publications, but as far as Django admin is concerned, I can only add one publication. Is there a similar way to do this as in ManyToManyField, where I can do

然而,这似乎不允许我向Author模型添加多个出版物。在这种情况下,所有的发布源可以有许多作者,而作者可以有许多发布,但是对于Django admin来说,我只能添加一个发布。有没有类似的方法来做这个在很多tomanyfield,我可以做

>>> a = Author()
>>> a.publications.add(Book())
>>> a.publications.add(Article())

1 个解决方案

#1


2  

You have to create the Many to Many association yourself.

你必须自己创造许多到许多的联系。

Soemthing like:

Soemthing:

class AuthorPublication(models.Model):
    author = models.ForeignKey(Author, related_name='publications')
    object_id = models.PositiveIntegerField()
    content_type = models.ForeignKey(ContentType)
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        unique_together = (
            ('object_id', 'content_type'),
        )

Then you can use it like so:

然后你可以这样使用:

>>> a = Author()
>>> a.publications.add(AuthorPublication(content_object=Book()))
>>> a.publications.add(AuthorPublication(content_object=Article()))

#1


2  

You have to create the Many to Many association yourself.

你必须自己创造许多到许多的联系。

Soemthing like:

Soemthing:

class AuthorPublication(models.Model):
    author = models.ForeignKey(Author, related_name='publications')
    object_id = models.PositiveIntegerField()
    content_type = models.ForeignKey(ContentType)
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        unique_together = (
            ('object_id', 'content_type'),
        )

Then you can use it like so:

然后你可以这样使用:

>>> a = Author()
>>> a.publications.add(AuthorPublication(content_object=Book()))
>>> a.publications.add(AuthorPublication(content_object=Article()))