2

Django 1.10

In my view, I have a function get_queryset() that, currently, returns one queryset. This function is called from another function, get_context(), which takes that data, uses it to get some values, and returns everything to the front end.

However, I now want to return 2 querysets from get_queryset(), one that is the full queryset and one that has a filter applied to it.

I assumed I could simply do something like:

full_results = query.all()
# do some filtering
filter_results = full_results.someFilter()

return full_results, filter_results

However, I have another function where these results get sent to FIRST before being sent to the front end. I figured I would be able to access these querysets easily with bracket notation, like this -

faceted = self.get_queryset()[0].facet('thing')

However! Turns out that I am unable to do so. I am trying to access a property on each item in the queryset in this second django function but am getting an error -

AttributeError: 'SearchQuerySet' object has no attribute 'feature'

So clearly I am doing something wrong. Is it possible to do what I am trying to do? Or would it be better to just make another function to return this data?

1
  • How about just using your view's get_context_data() function to do what it is you want to do? Commented Jul 9, 2018 at 15:57

1 Answer 1

4

You can only return a single queryset from get_queryset. But get_context_data itself can be extended to add whatever you like.

def get_context_data(self, *args, **kwargs):
    context = super().get_context_data(*args, **kwargs)
    context['filter_results'] = context['full_results'].someFilter()
    return context
Sign up to request clarification or add additional context in comments.

1 Comment

I see! I didn't think about doing it like this but this seems to be way easier. Thanks Daniel, gonna try this out!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.