Edited Title : Limit multiple "Many to One" fields into "One to One" association : Django
We've Book, User and Rating as Django model.
- Each Book has many Ratings
- Each Rating has one Book
- Each User has many Ratings
- Each Rating has one User
For Book
class Book(models.Model):
isbn = models.CharField(max_length=32)
title = models.CharField(max_length=256)
def __str__(self):
return self.title
For Book Rating
class BookRating(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
rating = models.SmallIntegerField(choices=[(i, i) for i in range(1, 6)])
def __str__(self):
return self.rating
Problem Statement
How can I ensure that each User has atmost one rating on each book?
Just do
Your models do implement a many to many relationship, however you are not getting full access to the django ManyToMany functionality. I recommend you do something like this:
When you do this your BookRating can remain unchanged, but the small change to the Book model gives you full access to the api described here: https://docs.djangoproject.com/en/1.10/topics/db/examples/many_to_many/
What's interesting is that modifying the Book model as described above does not make any changes to your table structures in your database. They remain unchanged. It's simply a matter of unlocking the api.