Elchinas
Elchinas

Reputation: 139

How to store per-user data in django permanently and efficiently

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:

Thank you very much for your time, I hope someone knows it!

Upvotes: 2

Views: 1560

Answers (1)

Ralf
Ralf

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

Related Questions