Unique foreign key pairs with Django

前端 未结 1 1399
南方客
南方客 2021-02-02 10:28

I have three models: products, users, and reviews.

A review is linked to a product and a user as follows:



        
相关标签:
1条回答
  • 2021-02-02 11:08

    Use unique_together to make sure that each user/product combination is unique:

    class Review(models.Model):
    
      class Meta:
    
        unique_together = ['user', 'product']
    
      user = models.ForeignKey(User)
      product = models.ForeignKey(Product)
    
    0 讨论(0)
提交回复
热议问题