From b7ca720ed437a3503c362dcb207b2b490ceaf3f5 Mon Sep 17 00:00:00 2001
From: Georges Dubus <georges.dubus@compiletoi.net>
Date: Mon, 26 Dec 2011 13:54:14 +0100
Subject: [PATCH] Added a get_default_instance method to BaseCreateView for
situation in which fiels left out of the form need to be
set.
---
django/views/generic/edit.py | 9 ++++++++-
tests/regressiontests/generic_views/edit.py | 9 +++++++++
tests/regressiontests/generic_views/urls.py | 2 ++
tests/regressiontests/generic_views/views.py | 7 +++++++
4 files changed, 26 insertions(+), 1 deletions(-)
diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
index 3cade52..0e3ae75 100644
a
|
b
|
class BaseCreateView(ModelFormMixin, ProcessFormView):
|
163 | 163 | |
164 | 164 | Using this base class requires subclassing to provide a response mixin. |
165 | 165 | """ |
| 166 | def get_default_instance(self): |
| 167 | """ |
| 168 | Returns the default instance for the object, in case you need to |
| 169 | set some fields that not in the form. |
| 170 | """ |
| 171 | return None |
| 172 | |
166 | 173 | def get(self, request, *args, **kwargs): |
167 | 174 | self.object = None |
168 | 175 | return super(BaseCreateView, self).get(request, *args, **kwargs) |
169 | 176 | |
170 | 177 | def post(self, request, *args, **kwargs): |
171 | | self.object = None |
| 178 | self.object = self.get_default_instance() |
172 | 179 | return super(BaseCreateView, self).post(request, *args, **kwargs) |
173 | 180 | |
174 | 181 | |
diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
index 182615a..5ce1f26 100644
a
|
b
|
class CreateViewTests(TestCase):
|
92 | 92 | self.assertEqual(res.status_code, 302) |
93 | 93 | self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/') |
94 | 94 | |
| 95 | def test_create_with_default_values(self): |
| 96 | res = self.client.post('/edit/authors/create/defaultinstance/', |
| 97 | {'name': 'Randall Munroe'}) |
| 98 | self.assertEqual(res.status_code, 302) |
| 99 | self.assertRedirects(res, 'http://testserver/list/authors/') |
| 100 | self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>']) |
| 101 | author = Author.objects.get(name='Randall Munroe') |
| 102 | self.assertEqual(author.slug, 'default-slug') |
| 103 | |
95 | 104 | class UpdateViewTests(TestCase): |
96 | 105 | urls = 'regressiontests.generic_views.urls' |
97 | 106 | |
diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
index 090ec73..84ee509 100644
a
|
b
|
urlpatterns = patterns('',
|
71 | 71 | views.AuthorCreate.as_view()), |
72 | 72 | (r'^edit/authors/create/special/$', |
73 | 73 | views.SpecializedAuthorCreate.as_view()), |
| 74 | (r'^edit/authors/create/defaultinstance/$', |
| 75 | views.DefaultInstanceAuthorCreate.as_view()), |
74 | 76 | |
75 | 77 | (r'^edit/author/(?P<pk>\d+)/update/naive/$', |
76 | 78 | views.NaiveAuthorUpdate.as_view()), |
diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
index 5ff9cf0..2f77254 100644
a
|
b
|
from __future__ import absolute_import
|
3 | 3 | from django.contrib.auth.decorators import login_required |
4 | 4 | from django.core.paginator import Paginator |
5 | 5 | from django.core.urlresolvers import reverse |
| 6 | from django.forms.models import modelform_factory |
6 | 7 | from django.utils.decorators import method_decorator |
7 | 8 | from django.views import generic |
8 | 9 | |
… |
… |
class AuthorCreateRestricted(AuthorCreate):
|
103 | 104 | post = method_decorator(login_required)(AuthorCreate.post) |
104 | 105 | |
105 | 106 | |
| 107 | class DefaultInstanceAuthorCreate(AuthorCreate): |
| 108 | form_class = modelform_factory(Author, fields=('name',)) |
| 109 | def get_default_instance(self): |
| 110 | return Author(slug='default-slug') |
| 111 | |
| 112 | |
106 | 113 | class ArtistUpdate(generic.UpdateView): |
107 | 114 | model = Artist |
108 | 115 | |