Opened 3 years ago

Last modified 3 months ago

#16970 new New feature

calling as_view of CBV in URLConf needs better documentation and examples

Reported by: pydanny Owned by: nobody
Component: Documentation Version: 1.3
Severity: Normal Keywords:
Cc: me@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Say I have a ListView that calls on a Thing model. Thing model has it's own Model Manager that adds query method called 'some_things'. In theory I code it thus:

url(regex=r'^$',
    view=ListView.as_view(
        queryset=Thing.objects.some_things(),
        template_name='things/some_things.html'),
    name='some_things',    
    ),    

However, (MultipleObjectMixin.get_queryset) https://code.djangoproject.com/browser/django/trunk/django/views/generic/list.py shows that if a model is not defined in the ListView arguments, that the default manager all method is called. Which means if you want to get the behavior you want you need to do:

url(regex=r'^$',
    view=ListView.as_view(
        model=Job,
        queryset=Thing.objects.some_things(),
        template_name='things/some_things.html'),
    name='some_things',    
    ),    

See how I added an explicit model call?

Figuring this out meant rooting through Django source code. What I propose is documenting this behavior in the pages on Class Based Views. I would do it myself but it is unclear where I should put said documentation (topic or not?).

Change History (9)

comment:1 Changed 3 years ago by pydanny

  • Component changed from Documentation to Generic views
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from ListView docs need to be more explicit in regards to model managers to ListView need to be more explicit in regards to model managers

In retrospect, this is not a documentation issue but a real bug. If a queryset is specified, why is it being overridden? So I call bug.

comment:2 Changed 3 years ago by pydanny

  • Type changed from Uncategorized to Bug

comment:3 Changed 3 years ago by pydanny

I'll be documenting this with a real sample of the bug in the next 24 hours. Just need to scrape the client specific parts from the code and paste it in.

comment:4 Changed 3 years ago by ptone

I'm assuming Job==Thing in your description.

One thing is that your queryset will be evaluated at URL import time - and that is probably not what you wanted.

Since you want to define a custom queryset for each time the view is visited, what you probably want is:

url(regex=r'^$',
    view=ListView.as_view(
        get_queryset=Thing.objects.some_things,
        template_name='things/some_things.html'),
    name='some_things',    
    ),  

which will result in your custom manager being called each time the view goes to get a list of objects (see the note in the docs "Thread safety with view arguments" in https://docs.djangoproject.com/en/1.3//ref/class-based-views/#django.views.generic.base.View)

Furthermore, the queryset attr is only replace with a call to the default manager if queryset is None, so I'm not sure how you would be seeing the default manager being called in your example, even if you also set the model

comment:5 Changed 3 years ago by pydanny

  • Component changed from Generic views to Documentation
  • Type changed from Bug to Uncategorized

I think it's pretty clear from @ptone's comment that this is not a bug then, it's a documentation issue. Changing the ticket type accordingly.

comment:6 Changed 3 years ago by ptone

  • Summary changed from ListView need to be more explicit in regards to model managers to calling as_view of CBV in URLConf needs better documentation and examples
  • Triage Stage changed from Unreviewed to Accepted

hoping to address this in #16807

comment:7 Changed 2 years ago by aaugustin

  • Type changed from Uncategorized to New feature

comment:8 Changed 3 months ago by iambibhas

  • Cc me@… added

#16807 was fixed long time back. Is this ticket still relevant?

comment:9 Changed 3 months ago by timgraham

I don't think that ticket ended up adding any clarification regarding the issues presented here.

Note: See TracTickets for help on using tickets.
Back to Top