Reputation: 2212
I'm slightly confused about what the "right way" is to design Django models for this particular use case.
I have models called Book, User, Rating. I'm designing an app that let users rate books.
Rating has a many-to-one relationship with user and a many-to-one relationship with Book.
class User(models.Model):
name = models.CharField(max_length=30)
class Book(models.Model):
name = models.CharField(max_length=30)
class Rating(models.Model):
user = models.ForeignKey(User,related_name=ratings)
book = models.ForeignKey(Book,related_name=ratings)
...
With this design, I can't work out how to enforce the rule that a user can only rate each book once. I'm also not sure whether this is the right way to design models for this use case. So if I could get a few pointers in the right direction, it might help me avoid design flaws at this early stage.
Thanks in advance!
Upvotes: 2
Views: 345
Reputation: 34553
You can use the unique_together Meta option on your Rating model: https://docs.djangoproject.com/en/1.3/ref/models/options/#unique-together
Upvotes: 5