Danial Tz
Danial Tz

Reputation: 1984

Performing extra operations on a model's final queryset

Normally, one can override get_context_data, get_queryset and get_object to manipulate and do other works (AFAIK) on a model object, here "book". The question is that how I can get whatever queryset is generated after the filtering is done, and extract useful information from it, to show in other parts of the page.

Let's say I have a simple model of book and publisher. I am trying to do get the main query, and put a count on it for a get_context_data object. But apparently, get_context gets the get_queryset before it is overridden by me, so my query is not the filtered one.

# models.py
from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    class Meta:
        ordering = ["-name"]

    def __unicode__(self):
        return self.name

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField('Author')
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()



class BookListView(ListView):

    context_object_name = "book"
    model = Book


    def get_queryset(self):
        """ filter the publishers based on city """
        qs = super(BookListView, self).get_queryset()

        city_list = self.request.GET.getlist(u'city', None)

        if len(city_list) > 0:
            qs = qs.filter(publisher__city__iregex=r'(' + '|'.join(city_list) + ')')

        return qs

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(PublisherDetailView, self).get_context_data(**kwargs)

        #### How to get the count of the books once filtered in get_queryset

        context['book_list'] = self.model.objects.values('publisher_name').annotate(
            num_cl=Count('publisher')) \
            .order_by("-num_cl")

        return context

Upvotes: 0

Views: 54

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

Why don't you use the queryset directly?

context['book_list'] = context['object_list'].annotate(...)

Also note that your queryset filter is very bizarre. Better to use __in:

if city_list:
    qs = qs.filter(publisher__city__in=[city_list])

Upvotes: 2

Related Questions