django – ‘unique_together’指的是不存在的字段

尝试在两个外键约束上使用unique_together时,我收到以下错误:

CommandError: System check identified some issues:

ERRORS:
main.AuthorTag: (models.E012) 'unique_together' refers to the non-existent field 'author'.
main.AuthorTag: (models.E012) 'unique_together' refers to the non-existent field 'tag'.

在下表中

class AuthorTag(models.Model):
    class Meta:
        unique_together = (('tag', 'author',),)
    tag = models.ForeignKey(TagIndex, on_delete=models.CASCADE),
    author = models.ForeignKey(Author, on_delete=models.CASCADE),

同样的例子似乎在这里工作:Unique foreign key pairs with Django
但我无法弄清楚为什么我会收到这个错误

编辑:
将unique_together =((‘tag’,’author’,)改为unique_together =((‘tag’,’author’))给我同样的错误.以及在字段声明下面移动元类.

最佳答案 删除尾随逗号,使代码为:

class AuthorTag(models.Model):
    class Meta:
        unique_together = ['tag', 'author']

    tag = models.ForeignKey(TagIndex, on_delete=models.CASCADE)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

使它工作.我不确定为什么.

点赞