Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question