Opened 10 years ago

Last modified 10 years ago

#24689 closed Bug

DetailView get_template_name and get_context_object_name fail with deferred QuerySet — at Version 5

Reported by: Bertrand Bordage Owned by: nobody
Component: Generic views Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Bertrand Bordage)

Suppose you define a view like this:

from django.views.generic import DetailView
from django.contrib.auth.models import User

class UserDetail(DetailView):
    model = User
    queryset = User.objects.defer('password')

If you try to use the view, the default template name is auth/user_deferred_password_detail.html instead of auth/user_detail.html.

This is because Django creates a proxy model each time you use defer/only. And therefore User.objects.defer('password')[0]._meta.model_name is 'user_deferred_password'.

DetailView.get_template_name should ckeck if model._deferred and use model._meta.proxy_for_model to get the original model.

Change History (5)

comment:1 by Bertrand Bordage, 10 years ago

Summary: DetailView & ListView get_template_name fails with deferred QuerySetDetailView & ListView get_template_name and get_context_object_name fail with deferred QuerySet

Another consequence of that deferred proxy model is that (DetailView|ListView).context_object_name is also modified.

comment:2 by Tim Graham, 10 years ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted

comment:3 by Artis Avotins, 10 years ago

Submitted a pull request on GitHub fixing this issue. Didn't occur when using ListView though. https://github.com/django/django/pull/4546

comment:4 by Tim Graham, 10 years ago

Has patch: set
Needs tests: set

comment:5 by Bertrand Bordage, 10 years ago

Description: modified (diff)
Summary: DetailView & ListView get_template_name and get_context_object_name fail with deferred QuerySetDetailView get_template_name and get_context_object_name fail with deferred QuerySet

Indeed, it’s not occurring using ListView. I assumed it would occur on it because I thought User.objects.defer('password') would be a queryset of the proxy model User_Deferred_password, but it’s not. It’s only once you get model instances that they use the proxy model.

Sorry for not checking :(

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