Skip to content

Commit

Permalink
Generic View's model can be found in form_class
Browse files Browse the repository at this point in the history
if it's a ModelForm
  • Loading branch information
Guilhem Saurel committed Mar 1, 2016
1 parent c003359 commit 91f221b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
9 changes: 9 additions & 0 deletions django/views/generic/detail.py
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

from django import forms
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.http import Http404
Expand Down Expand Up @@ -66,6 +67,8 @@ def get_queryset(self):
if self.queryset is None:
if self.model:
return self.model._default_manager.all()
elif hasattr(self, 'form_class') and issubclass(self.form_class, forms.ModelForm):
return self.form_class.Meta.model._default_manager.all()
else:
raise ImproperlyConfigured(
"%(cls)s is missing a QuerySet. Define "
Expand Down Expand Up @@ -165,6 +168,12 @@ def get_template_names(self):
self.model._meta.model_name,
self.template_name_suffix
))
elif hasattr(self, 'form_class') and issubclass(self.form_class, forms.ModelForm):
names.append("%s/%s%s.html" % (
self.form_class.Meta.model._meta.app_label,
self.form_class.Meta.model._meta.model_name,
self.template_name_suffix
))

# If we still haven't managed to find any template names, we should
# re-raise the ImproperlyConfigured to alert the user.
Expand Down
4 changes: 2 additions & 2 deletions tests/generic_views/test_edit.py
Expand Up @@ -184,7 +184,7 @@ def test_create_with_special_properties(self):
self.assertIsInstance(res.context['form'], views.AuthorForm)
self.assertNotIn('object', res.context)
self.assertNotIn('author', res.context)
self.assertTemplateUsed(res, 'generic_views/form.html')
self.assertTemplateUsed(res, 'generic_views/author_form.html')

res = self.client.post('/edit/authors/create/special/',
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
Expand Down Expand Up @@ -335,7 +335,7 @@ def test_update_with_special_properties(self):
self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
self.assertEqual(res.context['thingy'], Author.objects.get(pk=a.pk))
self.assertNotIn('author', res.context)
self.assertTemplateUsed(res, 'generic_views/form.html')
self.assertTemplateUsed(res, 'generic_views/author_form.html')

res = self.client.post('/edit/author/%d/update/special/' % a.pk,
{'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
Expand Down
4 changes: 0 additions & 4 deletions tests/generic_views/views.py
Expand Up @@ -123,9 +123,7 @@ class AuthorCreate(generic.CreateView):


class SpecializedAuthorCreate(generic.CreateView):
model = Author
form_class = AuthorForm
template_name = 'generic_views/form.html'
context_object_name = 'thingy'

def get_success_url(self):
Expand Down Expand Up @@ -166,9 +164,7 @@ def get_object(self):


class SpecializedAuthorUpdate(generic.UpdateView):
model = Author
form_class = AuthorForm
template_name = 'generic_views/form.html'
context_object_name = 'thingy'

def get_success_url(self):
Expand Down

0 comments on commit 91f221b

Please sign in to comment.