关系 - 每个用户每个地址评分一个

时间:2013-12-03 18:21:59

标签: django django-models foreign-keys relational-database

尝试建立这样的关系:

user / address的组合只能有一个rating,其中addressbuilding的一部分,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}}

目前无法正常运行,因为它允许每个用户每个地址有多个评级。

1 个答案:

答案 0 :(得分:1)

您可以添加unique_together约束,以便每个地址和用户组合都必须是唯一的

https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

unique_together = ('buildingaddress', 'author')