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

时间:2012-04-07 01:33:11

标签: django django-models

我有以下模型,通过指定的连接表发布需要一个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.

我的模特看起来像:

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()

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

1 个答案:

答案 0 :(得分:1)

  

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

您无法在absract模型上创建ForeignKey,因为该模型在DB中没有表,因此没有要引用的主键。因此,您应该改为Publication非摘要或引用Workshop。之后其他错误行也应该消失。