Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a specific field of a queryset in Django?

I have these models in my Django app:

class Book(models.Model):
    title = models.CharField(max_length=50, unique=True)
    owner = models.CharField(max_length=30)
    price = models.DecimalField(max_digits=5, decimal_places=2, null=True)
    book_rating = models.ForeignKey('Rating', null=True)

RATE_CHOICES = zip(range(1,6), range(1,6))
class Rating(models.Model):
    user = models.ForeignKey(User)
    this_book = models.ForeignKey(Book)
    rate = models.DecimalField(max_digits=2, decimal_places=1, choices=RATE_CHOICES)
    comment = models.TextField(max_length=4000, null=True)

I am trying to access the Ratings of each instance of the Book model. Here is what I've tried so far in the shell:

from django.contrib.contenttypes.models import ContentType
>>> ctype = ContentType.objects.get_for_model(Rating)
>>> ctype
<ContentType: rating>
>>> book_titles = ctype.model_class().objects.filter(this_book__title='My Test Book')
>>> book_titles
<QuerySet [<Rating: My Test Book - parrot987 - 3.0>, <Rating: My Test Book - [email protected] - 5.0>]>
  1. How can I access the two rating values of each object (5.0 and 3.0) without all of this other data?

  2. Can this be done in such a way that I am able to average the numbers and return the final value?

like image 245
Mathyou Avatar asked Dec 06 '22 13:12

Mathyou


1 Answers

For 1. you can use (relevant documentation):

Rating.objects.filter(this_book__title='My Test Book').values('rate')

If you just want a flat list you can use values_list('rate', flat=True) instead of values('rate').

For 2 (relevant documentation):

from django.db.models import Avg

Rating.objects.filter(this_book__title='My Test Book').aggregate(Avg('rate'))

This will return a dictionary where the key is rate__avg and the value is the average of the ratings.

like image 170
Paulo Almeida Avatar answered Dec 29 '22 00:12

Paulo Almeida