Home > database >  How do I selectively mark books read by a user in the frontend if I have a common dataset of books a
How do I selectively mark books read by a user in the frontend if I have a common dataset of books a

Time:12-07

I have a "Books" model in django which stores information like the name, author etc (irrelevant here, just mentioning for some context). I display all that information using a Paginator and each user has the option of marking the books he/she has read. To mark them, what I can think of doing is creating another model called "BookStatus" where I insert new entries every time a user reads a book. I also thought of doing this because I want to store additional information like the time at which the book was read and how long the user spent reading it. However, I'm unable to figure out how to actually display that the user has read some particular book next to the book's name while displaying it in the frontend. What would be the easiest way to accomplish this?

In short, I have a database of books which are common to all users. For each user I want to store the books he/she has read along with the time at which they read it and how long they took to do so (all entered by the user) and while displaying those common books I want to be able to mark them as read/unread on the frontend.

CodePudding user response:

I think you're on the right track. Here's an example of what you can do:

class BookStatus(models.Model):
    book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='book')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user')
    read = models.BooleanField(default=False)
    duration = models.DurationField(blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.user.username} is reading {self.book.name}"
  • Related