Domanda

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?

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top