Tuesday, October 17, 2017

Re: Tutorial part 4 - get_queryset



On Oct 17, 2017 4:44 AM, "tking" <tking@gmx.co.uk> wrote:

Hi,

I've decided to teach myself Django and I'm enjoying the tutorial which I've so far found exceptionally detailed but I'm a bit confused by the get_queryset function in the IndexView class.

Essentially my question is what calls this function?

I always recommend taking a look at the Classy CBV's site, it greatly helps demystify CBV's:


One if the disadvantages of CBV's is the unintuitive flow and how all of the pieces work together. It takes a bit of experience to get used to it. In short, though, it works like this for most CBV's:

1. The .as_view() call from urls.py calls .dispatch().
2. The .dispatch() calls then calls the appropriate function based on the HTTP verb that was used in the request, usually .get().
3. The .get() call then calls .get_queryset().
4. The .get_queryset() call then either returns the value of self.queryset, or returns a generic Model.objects.all() (based on self.model) as a fallback.



Is it automatically called when the class is used?  I appreciate this is part of a generic view and it's a built-in function, so that makes some sense to me, but it's not clear.

Yes, in most cases, if a CBV is used. See above.


I've looked elsewhere in the documentation, at the generic views in particular and whilst get_queryset is used in another example, I see that it isn't a mandatory requirement, as other examples have just a model or queryset assignment, the latter of which shows a filtered assignment.

So is the get_queryset just a way of automatically creating a queryset attribute?

By default, it will return the value of queryset= if set, otherwise it will return a generic queryset of .all() based on your model= attribute.

This method is often overridden by custom views to modify the queryset and include things like custom sorting and filtering based on GET or POST values from the request.


I'm not seeing any advantage to this at present to a more simple, queryset = 

It's a shortcut way to define a custom static queryset in a view.

Say that you had a view that listed only activated Widgets, which had a model field of 'activated' that was set to True if a particular Widget was activated. Your view can then set your queryset like this:

queryset = Widget.objects.filter(activated=True)

And with a single line in the view, you can now display a list of Widget objects that are activated, rather than listing all of them.

HTH,

-James

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciX0mS_TE4mxqvptPL1M9MvWXw2egsu43JdSn5-Hr8hVoQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment