B
B
blackbb2017-04-23 00:41:45
Django
blackbb, 2017-04-23 00:41:45

How to filter by field in ListView in Django?

There are models:

class School(models.Model):
    slug = models.SlugField(unique=True, verbose_name='ЧПУ')
    title = models.CharField(max_length=300, verbose_name='Название')

class Review(models.Model):
    school = models.ForeignKey(School, verbose_name='Школа')
    RATING_CHOICES = (
    (1, '1'),
    (2, '2'),
    (3, '3'),
    (4, '4'),
    (5, '5'),
    )
    rating = models.IntegerField(choices=RATING_CHOICES, default='5')

class SchoolList(ListView):
    model = School
    def get_context_data(self, **kwargs):
        context = super(SchoolList, self).get_context_data(**kwargs)
        context['review_count'] =  Review.objects.filter(school__slug=???).count().aggregate(Sum('rating')).values()[0] /Review.objects.filter(school__slug=???).count()
        return context

Displays a list of schools. Schools have reviews, so I want to display the count of reviews for each school in the list. I need to filter the Review by the schools in the list, I can't catch up on how to do it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question