Reputation: 139
I am working on a websie in django about books, where you can post reviews, rate the books etc... I'd like to know how to store data about every user in an efficient way, as to know whether the user:
This way, no user could post 2 reviews and I could provide a per-user experience. I even want to limit how many times they can rate (like 3 every month). Problems:
Sessions do not seem useful since the data is not stored in the db and once the user exits and reenters the page, the session is gone (I checked the django docs but the example doesn't seem useful for me).
I thought about adding dicts or lists in every instance of the model, and storing there all the necessary data, but I don't know whether it is appropriate/efficient
Thank you very much for your time, I hope someone knows it!
Upvotes: 2
Views: 1560
Reputation: 16495
To your first goal:
Has already posted a review for a specific book
You can create a new model that has
class Review(models.Model):
user = models.ForeignKey(...)
book = models.ForeignKey(...)
datetime = models.DateTimeField()
... other fields like review content or score ...
class Meta:
unique_together = [
('user', 'book'),
]
To your second goal:
Has already rated a book
You could add another model "Ratings" with a similar structure, or you could add that info to your "Review" model since a single user can only give one review and one rating for each book.
For your third goal:
I even want to limit how many times they can rate (like 3 every month).
A very simple way could be to add three DateFields to your users profile (read about customizing Djangos User model) to record the last times each User gave ratings.
Or you could query the "Rating" model every time the user wants to make a new rating to check if sufficient time has past since the last entries for that user.
Upvotes: 1