Question

I have the following models:

class Book(models.Model):    
    ASIN = models.CharField(max_length=255, primary_key=True)
    title = models.CharField(max_length=255)
    users = models.ManyToManyField(FacebookCustomUser, through='BookRatings')

class BookRatings(models.Model):
    user = models.ForeignKey(FacebookCustomUser)
    book = models.ForeignKey(Book)
    rating = models.IntegerField()

Now I want to get all books and ratings for a logged user. I tried the following:

books = request.user.book_set.all()

But now in books is no field for the rating. What am I doing wrong?

Was it helpful?

Solution

book_set gives you the Book record, not the BookRating one. You can get that via the standard reverse relationship:

request.user.bookratings_set.all()

That will have the rating field, plus the book ForeignKey to get the Book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top