尝试建立这样的关系:
user
/ address
的组合只能有一个rating
,其中address
是building
的一部分,class Rating(models.Model):
buildingaddress = select2.fields.ForeignKey(BuildingAddress, overlay='Select the Building Address')
building = select2.fields.ForeignKey(Building, db_column='bin', null=True, overlay='Select your Building')
author = select2.fields.ForeignKey(User, overlay='Select the Author')
suggestion = models.TextField()
rating = models.IntegerField(null=True, blank=True)
也是外键评级。
目前我有这个:
{{1}}
目前无法正常运行,因为它允许每个用户每个地址有多个评级。
答案 0 :(得分:1)
您可以添加unique_together
约束,以便每个地址和用户组合都必须是唯一的
https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together
unique_together = ('buildingaddress', 'author')