Code

Ticket #15125: introspec_meta.2.diff

File introspec_meta.2.diff, 2.8 KB (added by sontek, 3 years ago)
Line 
1Index: django/views/generic/edit.py
2===================================================================
3--- django/views/generic/edit.py        (revision 15248)
4+++ django/views/generic/edit.py        (working copy)
5@@ -86,6 +86,17 @@
6         kwargs.update({'instance': self.object})
7         return kwargs
8 
9+    def get_queryset(self):
10+        """
11+        Get the queryset to look an object up against. May not be called if
12+        `get_object` is overridden.
13+        """
14+        if self.queryset is None:
15+            if self.form_class:
16+                return self.form_class._meta.model._default_manager.all()
17+            else:
18+                return super(ModelFormMixin, self).get_queryset()
19+
20     def get_success_url(self):
21         if self.success_url:
22             url = self.success_url % self.object.__dict__
23Index: tests/regressiontests/generic_views/edit.py
24===================================================================
25--- tests/regressiontests/generic_views/edit.py (revision 15248)
26+++ tests/regressiontests/generic_views/edit.py (working copy)
27@@ -201,6 +201,17 @@
28         except ImproperlyConfigured:
29             pass
30 
31+    def test_update_with_form_class(self):
32+        a = Author.objects.create(
33+            name='Randall Munroe',
34+            slug='randall-munroe',
35+        )
36+        res = self.client.post('/edit/author/%d/update/formclass/' % a.pk,
37+                        {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
38+        self.assertEqual(res.status_code, 302)
39+        self.assertRedirects(res, 'http://testserver/edit/authors/create/')
40+        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
41+
42 class DeleteViewTests(TestCase):
43     urls = 'regressiontests.generic_views.urls'
44 
45Index: tests/regressiontests/generic_views/urls.py
46===================================================================
47--- tests/regressiontests/generic_views/urls.py (revision 15248)
48+++ tests/regressiontests/generic_views/urls.py (working copy)
49@@ -1,5 +1,6 @@
50 from django.conf.urls.defaults import *
51-from django.views.generic import TemplateView
52+from django.views.generic import TemplateView, UpdateView
53+from regressiontests.generic_views.forms import AuthorForm
54 
55 import views
56 
57@@ -64,6 +65,9 @@
58         views.NaiveAuthorUpdate.as_view()),
59     (r'^edit/author/(?P<pk>\d+)/update/redirect/$',
60         views.NaiveAuthorUpdate.as_view(success_url='/edit/authors/create/')),
61+    (r'^edit/author/(?P<pk>\d+)/update/formclass/$',
62+        UpdateView.as_view(form_class=AuthorForm,
63+            success_url='/edit/authors/create/')),
64     (r'^edit/author/(?P<pk>\d+)/update/interpolate_redirect/$',
65         views.NaiveAuthorUpdate.as_view(success_url='/edit/author/%(id)d/update/')),
66     (r'^edit/author/(?P<pk>\d+)/update/$',