Jay P.
Jay P.

Reputation: 2590

How to pass value from get_queryset() to get_context_data()

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

Upvotes: 2

Views: 2776

Answers (3)

apet
apet

Reputation: 1098

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
        .....

Upvotes: 6

Mahender Thakur
Mahender Thakur

Reputation: 510

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

Upvotes: 0

Alasdair
Alasdair

Reputation: 308899

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 %}

Upvotes: 3

Related Questions