Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #15247 -- Ensured that if a SingleObject view defines get_objec…

…t but not get_queryset, the ModelFormMixin doesn't fail. Thanks to Sergey N. Belinsky for the report and test case.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15540 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 64b042bb3c017e327edc326ce370a16a910ea4a6 1 parent 4bb2db0
@freakboy3742 freakboy3742 authored
View
12 django/views/generic/edit.py
@@ -75,7 +75,17 @@ def get_form_class(self):
if self.form_class:
return self.form_class
else:
- model = self.get_queryset().model
+ if self.model is not None:
+ # If a model has been explicitly provided, use it
+ model = self.model
+ elif hasattr(self, 'object') and self.object is not None:
+ # If this view is operating on a single object, use
+ # the class of that object
+ model = self.object.__class__
+ else:
+ # Try to get a queryset and extract the model class
+ # from that
+ model = self.get_queryset().model
return model_forms.modelform_factory(model)
def get_form_kwargs(self):
View
19 tests/regressiontests/generic_views/edit.py
@@ -201,6 +201,25 @@ def test_update_without_redirect(self):
except ImproperlyConfigured:
pass
+ def test_update_get_object(self):
+ a = Author.objects.create(
+ name='Randall Munroe',
+ slug='randall-munroe',
+ )
+ res = self.client.get('/edit/author/update/')
+ self.assertEqual(res.status_code, 200)
+ self.assertTrue(isinstance(res.context['form'], forms.ModelForm))
+ self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
+ self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
+ self.assertTemplateUsed(res, 'generic_views/author_form.html')
+
+ # Modification with both POST and PUT (browser compatible)
+ res = self.client.post('/edit/author/update/',
+ {'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
+ self.assertEqual(res.status_code, 302)
+ self.assertRedirects(res, 'http://testserver/list/authors/')
+ self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
+
class DeleteViewTests(TestCase):
urls = 'regressiontests.generic_views.urls'
View
2  tests/regressiontests/generic_views/urls.py
@@ -74,6 +74,8 @@
views.NaiveAuthorUpdate.as_view(success_url='/edit/author/%(id)d/update/')),
(r'^edit/author/(?P<pk>\d+)/update/$',
views.AuthorUpdate.as_view()),
+ (r'^edit/author/update/$',
+ views.OneAuthorUpdate.as_view()),
(r'^edit/author/(?P<pk>\d+)/update/special/$',
views.SpecializedAuthorUpdate.as_view()),
(r'^edit/author/(?P<pk>\d+)/delete/naive/$',
View
7 tests/regressiontests/generic_views/views.py
@@ -114,6 +114,13 @@ class AuthorUpdate(generic.UpdateView):
success_url = '/list/authors/'
+class OneAuthorUpdate(generic.UpdateView):
+ success_url = '/list/authors/'
+
+ def get_object(self):
+ return Author.objects.get(pk=1)
+
+
class SpecializedAuthorUpdate(generic.UpdateView):
model = Author
form_class = AuthorForm
Please sign in to comment.
Something went wrong with that request. Please try again.