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 Georges Dubus, 13 years ago)

First patch, without documentation

  • django/views/generic/edit.py

    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):  
    163163
    164164    Using this base class requires subclassing to provide a response mixin.
    165165    """
     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
    166173    def get(self, request, *args, **kwargs):
    167174        self.object = None
    168175        return super(BaseCreateView, self).get(request, *args, **kwargs)
    169176
    170177    def post(self, request, *args, **kwargs):
    171         self.object = None
     178        self.object = self.get_default_instance()
    172179        return super(BaseCreateView, self).post(request, *args, **kwargs)
    173180
    174181
  • tests/regressiontests/generic_views/edit.py

    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):  
    9292        self.assertEqual(res.status_code, 302)
    9393        self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
    9494
     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
    95104class UpdateViewTests(TestCase):
    96105    urls = 'regressiontests.generic_views.urls'
    97106
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index 090ec73..84ee509 100644
    a b urlpatterns = patterns('',  
    7171        views.AuthorCreate.as_view()),
    7272    (r'^edit/authors/create/special/$',
    7373        views.SpecializedAuthorCreate.as_view()),
     74    (r'^edit/authors/create/defaultinstance/$',
     75        views.DefaultInstanceAuthorCreate.as_view()),
    7476
    7577    (r'^edit/author/(?P<pk>\d+)/update/naive/$',
    7678        views.NaiveAuthorUpdate.as_view()),
  • tests/regressiontests/generic_views/views.py

    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  
    33from django.contrib.auth.decorators import login_required
    44from django.core.paginator import Paginator
    55from django.core.urlresolvers import reverse
     6from django.forms.models import modelform_factory
    67from django.utils.decorators import method_decorator
    78from django.views import generic
    89
    class AuthorCreateRestricted(AuthorCreate):  
    103104    post = method_decorator(login_required)(AuthorCreate.post)
    104105
    105106
     107class DefaultInstanceAuthorCreate(AuthorCreate):
     108    form_class = modelform_factory(Author, fields=('name',))
     109    def get_default_instance(self):
     110        return Author(slug='default-slug')
     111
     112
    106113class ArtistUpdate(generic.UpdateView):
    107114    model = Artist
    108115
Back to Top