1

我有以下模型,需要通过指定的连接表与作者一起发布 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()

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

4

1 回答 1

1

Publications.authorsjointable:“unique_together”指的是人员,一个不存在的字段。检查你的语法。

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

于 2012-04-07T02:51:10.063 回答