django中多对多连接表中的参考抽象模型

时间:2021-03-25 20:16:52

I have the following models, with publication needing a m2m with authors via the join table specified, I have done this but keep getting the error:

我有以下模型,发布需要一个m2m与作者通过指定的连接表,我已经做到这一点,但不断得到错误:

Error: One or more models did not validate:
publications.workshop: 'staff' is a manually-defined m2m relation through model AuthorsJoinTable, which does not have foreign keys to Staff and Workshop
publications.technicalreport: 'staff' is a manually-defined m2m relation through model          AuthorsJoinTable, which does not have foreign keys to Staff and TechnicalReport
publications.authorsjointable: 'publication' has a relation with model Publication, which has either not been installed or is abstract.
publications.authorsjointable: "unique_together" refers to staff, a field that doesn't exist. Check your syntax.

My models look like:

我的模型看起来像:

class Publication(models.Model):
    title = models.CharField(max_length=500)
    staff = models.ManyToManyField("personnel.Staff", related_name='%(app_label)s_%(class)s_related', through='AuthorsJoinTable')
    tag = models.ManyToManyField("Tag", related_name='%(app_label)s_%(class)s_related')
    class Meta:
        abstract = True

class Workshop(Publication):
    location = models.CharField(max_length=100)
    workshop_title = models.CharField(max_length=100)
    start_date = models.DateField()
    end_date = models.DateField()
    def __unicode__(self):  
        return u'%s - %s' % (self.title, self.workshoptitle)

class TechnicalReport(Publication):
    published_date = models.DateField()

class AuthorsJoinTable(models.Model):
    author = models.ForeignKey("Author", related_name='%(app_label)s_%(class)s_from')
    publication = models.ForeignKey("Publication", related_name='%(app_label)s_%(class)s_to')
    order = models.IntegerField()
    class Meta:
        unique_together = ('staff', 'publication')

class Tag(models.Model):
    tag_name = models.CharField(max_length=100, primary_key=True)

class Author(models.Model):
    name = models.CharField(max_length=100)
    biography = models.TextField()

So how can I resolve this problem?

那么我该如何解决这个问题呢?

1 个解决方案

#1


1  

publications.authorsjointable: "unique_together" refers to staff, a field that doesn't exist. Check your syntax.

publications.authorsjointable:“unique_together”指的是员工,一个不存在的领域。检查你的语法。

You can't create a ForeignKey on absract model because that model does not have a table in DB and therefore does not have primary key to reference. So you should make your Publication non-abstract or reference Workshop instead. Other error lines should also be gone after that.

您不能在absract模型上创建ForeignKey,因为该模型在DB中没有表,因此没有要引用的主键。因此,您应该使您的出版物非抽象或参考Workshop。之后其他错误行也应该消失。

#1


1  

publications.authorsjointable: "unique_together" refers to staff, a field that doesn't exist. Check your syntax.

publications.authorsjointable:“unique_together”指的是员工,一个不存在的领域。检查你的语法。

You can't create a ForeignKey on absract model because that model does not have a table in DB and therefore does not have primary key to reference. So you should make your Publication non-abstract or reference Workshop instead. Other error lines should also be gone after that.

您不能在absract模型上创建ForeignKey,因为该模型在DB中没有表,因此没有要引用的主键。因此,您应该使您的出版物非抽象或参考Workshop。之后其他错误行也应该消失。