2

As get_queryset() returns only one queryset and I need the length of the queryset search_store to the template file. So, I'm trying to send the value to the template through get_context_data.

I know I can get a length of a queryset through {{ queryset|length }}, but for some reason, it only returns a length of queryset separated by pagination, so I only get a partial length.

As you see the code, I'm trying to print search_stores.count(), and I need get it in get_context_data from get_queryset. Can anyone let me know how I can do that?

class SearchListView(ListView):
    model = Store
    template_name = 'boutique/search.html'
    paginate_by = 2
    context_object_name = 'search_stores'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['search_text'] = self.request.GET.get('search_text')
        context['sorter'] = self.request.GET.get('sorter')
        if not context['sorter']:
            context['sorter'] = 'popularity'
        return context

    def get_queryset(self):

        search_text = self.request.GET.get('search_text')
        sorter = self.request.GET.get('sorter')

        if not sorter:
            sorter = 'popularity'

        if search_text:
            search_stores = Store.objects.filter(Q(businessName__icontains=search_text) | Q(mKey__icontains=search_text))
            if sorter == 'businessName':
                search_stores = search_stores.order_by(sorter)
            else:
                search_stores = search_stores.order_by(sorter).reverse()
        else:
            search_stores = ''

        for store in search_stores:
            store.mKey = store.mKey.split(' ')

        print(search_stores.count())
        return search_stores
Jay P.
  • 2,420
  • 6
  • 36
  • 71

3 Answers3

6

if question is "How to pass value from get_queryset() to get_context_data()", then get_queryset() returns object_list and you can access it following way:

    def get_context_data(self):
        .....
        something = self.object_list
        .....
Anupam
  • 14,950
  • 19
  • 67
  • 94
apet
  • 958
  • 14
  • 16
  • Thanks. Exactly the answer I needed. – hleroy May 14 '21 at 20:25
  • If you are using **pagination** you can access the *current page*'s object list by using `context['page_obj']` within `get_context_data()`. Ref: https://docs.djangoproject.com/en/4.0/topics/pagination/#paginating-a-listview – Anupam Dec 22 '21 at 10:38
3

When a list view is paginated, you can access the paginator in the template context with paginator.

Therefore you can access the length of the entire (unpaginated) queryset with:

{{ paginator.count }}

As an aside, your get_queryset method should always return a list or a queryset. When search_text is not specified, you could return an empty queryset, Store.objects.none().

def get_queryset(self):
    ...

    if search_text:
        ...
    else:
        search_stores = Store.objects.none()

    return search_stores

I would also avoid looping over the queryset to set store.mKey = store.mKey.split(' '). This will evaluate the entire queryset, not just the page that you are displaying. It might be better to add a property to your model, for example:

class Store(models.Model):
    ...

    @property
    def mkey_list(self):
        return self.mKey.split(' ')

Then in your template you can do something like:

{% for key in store.mkey_list %}
   {{ key }}
{% endfor %}
Alasdair
  • 298,606
  • 55
  • 578
  • 516
0

You can use {{ queryset.count }} instead of {{ queryset|length }}

Mahender Thakur
  • 510
  • 4
  • 13
  • doesn't this boil down to the same? it will always show the number of objects of the current page... whereas @Jay actually wants the total – Lemayzeur May 08 '18 at 20:02
  • Yes, correct I need the total number, but length returns only partial number based on each pagination. Also, I don't know why but count returns nothing – Jay P. May 08 '18 at 20:19
  • count works perfectly for me , just tried ! ...... count uses SQL request with count(*) – Mahender Thakur May 08 '18 at 20:29
  • Yeah, usually count worked for me too, but I don't know why it just returns nothing here.. – Jay P. May 08 '18 at 20:38