Tutorial part 4 - get_queryset

40 weergaven
Naar het eerste ongelezen bericht

tking

ongelezen,
17 okt 2017, 07:45:1117-10-2017
aan Django users

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?

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.

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?

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

I guess I need to finish the tutorial but if someone could help me understand how this fit together, I would be most grateful.

Regards,

  Tony

James Schneider

ongelezen,
17 okt 2017, 08:52:3017-10-2017
aan django...@googlegroups.com


On Oct 17, 2017 4:44 AM, "tking" <tk...@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

Tony King

ongelezen,
17 okt 2017, 10:06:5717-10-2017
aan Django users

Thanks James, that is a big help and I'm sure the link will provide some worthwhile extra reading, although I've clearly got a lot to learn anyway.

Regards,

  Tony

Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten