Code

Ticket #17464: Added-a-get_default_instance-method-to-BaseCreateVie.diff

File Added-a-get_default_instance-method-to-BaseCreateVie.diff, 4.0 KB (added by madjar, 2 years ago)

First patch, without documentation

Line 
1From b7ca720ed437a3503c362dcb207b2b490ceaf3f5 Mon Sep 17 00:00:00 2001
2From: Georges Dubus <georges.dubus@compiletoi.net>
3Date: Mon, 26 Dec 2011 13:54:14 +0100
4Subject: [PATCH] Added a get_default_instance method to BaseCreateView for
5 situation in which fiels left out of the form need to be
6 set.
7
8---
9 django/views/generic/edit.py                 |    9 ++++++++-
10 tests/regressiontests/generic_views/edit.py  |    9 +++++++++
11 tests/regressiontests/generic_views/urls.py  |    2 ++
12 tests/regressiontests/generic_views/views.py |    7 +++++++
13 4 files changed, 26 insertions(+), 1 deletions(-)
14
15diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
16index 3cade52..0e3ae75 100644
17--- a/django/views/generic/edit.py
18+++ b/django/views/generic/edit.py
19@@ -163,12 +163,19 @@ class BaseCreateView(ModelFormMixin, ProcessFormView):
20 
21     Using this base class requires subclassing to provide a response mixin.
22     """
23+    def get_default_instance(self):
24+        """
25+        Returns the default instance for the object, in case you need to
26+        set some fields that not in the form.
27+        """
28+        return None
29+
30     def get(self, request, *args, **kwargs):
31         self.object = None
32         return super(BaseCreateView, self).get(request, *args, **kwargs)
33 
34     def post(self, request, *args, **kwargs):
35-        self.object = None
36+        self.object = self.get_default_instance()
37         return super(BaseCreateView, self).post(request, *args, **kwargs)
38 
39 
40diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
41index 182615a..5ce1f26 100644
42--- a/tests/regressiontests/generic_views/edit.py
43+++ b/tests/regressiontests/generic_views/edit.py
44@@ -92,6 +92,15 @@ class CreateViewTests(TestCase):
45         self.assertEqual(res.status_code, 302)
46         self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
47 
48+    def test_create_with_default_values(self):
49+        res = self.client.post('/edit/authors/create/defaultinstance/',
50+                        {'name': 'Randall Munroe'})
51+        self.assertEqual(res.status_code, 302)
52+        self.assertRedirects(res, 'http://testserver/list/authors/')
53+        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
54+        author = Author.objects.get(name='Randall Munroe')
55+        self.assertEqual(author.slug, 'default-slug')
56+
57 class UpdateViewTests(TestCase):
58     urls = 'regressiontests.generic_views.urls'
59 
60diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
61index 090ec73..84ee509 100644
62--- a/tests/regressiontests/generic_views/urls.py
63+++ b/tests/regressiontests/generic_views/urls.py
64@@ -71,6 +71,8 @@ urlpatterns = patterns('',
65         views.AuthorCreate.as_view()),
66     (r'^edit/authors/create/special/$',
67         views.SpecializedAuthorCreate.as_view()),
68+    (r'^edit/authors/create/defaultinstance/$',
69+        views.DefaultInstanceAuthorCreate.as_view()),
70 
71     (r'^edit/author/(?P<pk>\d+)/update/naive/$',
72         views.NaiveAuthorUpdate.as_view()),
73diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
74index 5ff9cf0..2f77254 100644
75--- a/tests/regressiontests/generic_views/views.py
76+++ b/tests/regressiontests/generic_views/views.py
77@@ -3,6 +3,7 @@ from __future__ import absolute_import
78 from django.contrib.auth.decorators import login_required
79 from django.core.paginator import Paginator
80 from django.core.urlresolvers import reverse
81+from django.forms.models import modelform_factory
82 from django.utils.decorators import method_decorator
83 from django.views import generic
84 
85@@ -103,6 +104,12 @@ class AuthorCreateRestricted(AuthorCreate):
86     post = method_decorator(login_required)(AuthorCreate.post)
87 
88 
89+class DefaultInstanceAuthorCreate(AuthorCreate):
90+    form_class = modelform_factory(Author, fields=('name',))
91+    def get_default_instance(self):
92+        return Author(slug='default-slug')
93+
94+
95 class ArtistUpdate(generic.UpdateView):
96     model = Artist
97 
98--
991.7.8.1
100