#24689 closed Bug (fixed)
DetailView get_template_name and get_context_object_name fail with deferred QuerySet
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 )
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 (7)
comment:1 by , 10 years ago
Summary: | DetailView & ListView get_template_name fails with deferred QuerySet → DetailView & ListView get_template_name and get_context_object_name fail with deferred QuerySet |
---|
comment:2 by , 10 years ago
Easy pickings: | unset |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 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 , 10 years ago
Has patch: | set |
---|---|
Needs tests: | set |
comment:5 by , 10 years ago
Description: | modified (diff) |
---|---|
Summary: | DetailView & ListView get_template_name and get_context_object_name fail with deferred QuerySet → DetailView 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 :(
Another consequence of that deferred proxy model is that
(DetailView|ListView).context_object_name
is also modified.