Benefit of 'return Foo.objects.all()' in a ListView

41 views
Skip to first unread message

Richard Jackson

unread,
Feb 24, 2017, 6:51:16 PM2/24/17
to Django users
Hi there,

I'm starting to learn about Django views and wanted to ground my understanding.

What is the advantage of including the get_queryset(self) function in a ListView? When I've run it with a very basic model ('Foo') there doesn't appear to be any difference with/without it - this could well be that my examples are currently too basic.

Essentially, what is the difference between the two below:

# view.py
from .models import Foo
from django.views.generic import ListView


class IndexView(ListView):
    model = Foo

    def get_queryset(self):
        return Foo.objects.all()

vs

# view.py
from .models import Foo
from django.views.generic import ListView


class IndexView(ListView):
    model = Foo

Thanks!

Rich

Melvyn Sopacua

unread,
Feb 25, 2017, 4:32:32 AM2/25/17
to django...@googlegroups.com
Hi Richard,

On Friday 24 February 2017 15:51:15 Richard Jackson wrote:

> What is the advantage of including the get_queryset(self) function in
> a ListView? When I've run it with a very basic model ('Foo') there
> doesn't appear to be any difference with/without it - this could well
> be that my examples are currently too basic.

There is none. The benefit of the get_queryset() method is that you can
return something other than Foo.objects.all(). For example:

class MyBlogPosts(generic.ListView):
def get_queryset(self, request):
return self.model.objects.filter(user=self.request.user)
--
Melvyn Sopacua

Richard Jackson

unread,
Feb 25, 2017, 6:01:36 AM2/25/17
to Django users
Grand, thank you for clarifying!

Richard Jackson

unread,
Feb 25, 2017, 6:42:11 AM2/25/17
to Django users
Apologies if this is too off-topic, but in the case of using a get_queryset() as below:

class MyBlogPosts(generic.ListView):
        def get_queryset(self, request):
                return self.model.objects.filter(user=self.request.user)

...is it possible to return two uniquely identifiable sets - for example, if a model has a function which returns True or False, to return one set for all the True and one for all the False?

Right now I'm splitting them in the template in something such as the below:

<h2>True ones</h2>
{% for p in object_list %} <!-- This would ideally be a set called 'object_list_true' -->
    <ul>
        {% if p.true_or_false == True %}
            <li>{{ p.foo }}</li>
        {% endif %}
    </ul>
{% endfor %}

<h2>False ones</h2>
{% for p in object_list %} <!-- This would ideally be a set called 'object_list_false' -->
    <ul>
        {% if p.true_or_false == False %}
            <li>{{ p.foo }}</li>
        {% endif %}
    </ul>
{% endfor %}


Thanks, and apologies for the constant basic questions!

Rich



On Saturday, February 25, 2017 at 9:32:32 AM UTC, Melvyn Sopacua wrote:

ludovic coues

unread,
Feb 25, 2017, 6:57:37 AM2/25/17
to django...@googlegroups.com
That's what regroup is for
https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#regroup
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b8556d2f-079a-4ba0-9697-0dbaaa07823d%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Melvyn Sopacua

unread,
Feb 25, 2017, 9:14:10 AM2/25/17
to django...@googlegroups.com

Hi Richard,

 

Don't be discouraged to ask questions. It's what the list is for.

 

Ludovic is right, you'd normally use the regroup tag. But let's say you wanted to do this, then get_queryset isn't the right method.

It should return a queryset, and one only.

 

On Saturday 25 February 2017 03:42:11 Richard Jackson wrote:

 

> class MyBlogPosts(generic.ListView):

> def get_queryset(self, request):

 

My bad: request is not an argument here.

 

> ...is it possible to return two uniquely identifiable sets - for

> example, if a model has a function which returns True or False, to

> return one set for all the True and one for all the False?

 

General overview:

 

The goal of a View is to turn a HTTP request into a HTTP response. A Template provides context to the template,

A ListView uses a Template to do that and is responsible for providing a list of ModelInstances to the template. The get_queryset() method is used to retrieve those instances.

 

So, the logical place to provide a different grouping of a queryset is in the part where it gets sent to the template:

 

class BlogRoll(generic.ListView):

def get_context_data(self, **kwargs):

# let ListView do the hard work first

context = super(BlogRoll, self).get_context_data(**kwargs)

# Now make two groups

qs = context['object_list']

group1 = [ obj for obj in qs if obj.boolean_method() is True ]

group2 = [ obj for obj in qs if obj.boolean_method() is False ]

# update the context

context.update(

{ 'group1': group1, 'group2': group2 }

)

# and return it

return context

 

 

--

Melvyn Sopacua

Richard Jackson

unread,
Feb 27, 2017, 5:03:22 AM2/27/17
to Django users
Brilliant, thanks both, I've got it working in both ways!

Much appreciated,

Rich
Reply all
Reply to author
Forward
0 new messages