2012-04-07 42 views
1

我有以下的机型,拥有出版物需要通过指定的连接表authors用于M2M,我已经做到了这一点,但不断收到错误:参考抽象模型中加入表许多一对多在Django

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

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

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