Fixed #21967: Added check for object in ModelFormMixin.get_form_kwargs.

Thanks lagovas.lagovas at gmail.com for the report.
This commit is contained in:
Ryan Kaskel 2014-02-09 10:53:23 +00:00 committed by Tim Graham
parent 80cd64ee17
commit 75a96f06e9
2 changed files with 9 additions and 2 deletions

View File

@ -122,6 +122,7 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
Returns the keyword arguments for instantiating the form. Returns the keyword arguments for instantiating the form.
""" """
kwargs = super(ModelFormMixin, self).get_form_kwargs() kwargs = super(ModelFormMixin, self).get_form_kwargs()
if hasattr(self, 'object'):
kwargs.update({'instance': self.object}) kwargs.update({'instance': self.object})
return kwargs return kwargs

View File

@ -9,7 +9,7 @@ from django import forms
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from django.views.generic.base import View from django.views.generic.base import View
from django.views.generic.edit import FormMixin, CreateView from django.views.generic.edit import FormMixin, ModelFormMixin, CreateView
from . import views from . import views
from .models import Artist, Author from .models import Artist, Author
@ -55,6 +55,12 @@ class ModelFormMixinTests(TestCase):
form_class = views.AuthorGetQuerySetFormView().get_form_class() form_class = views.AuthorGetQuerySetFormView().get_form_class()
self.assertEqual(form_class._meta.model, Author) self.assertEqual(form_class._meta.model, Author)
def test_get_form_checks_for_object(self):
mixin = ModelFormMixin()
mixin.request = RequestFactory().get('/')
self.assertEqual({'initial': {}, 'prefix': None},
mixin.get_form_kwargs())
class CreateViewTests(TestCase): class CreateViewTests(TestCase):
urls = 'generic_views.urls' urls = 'generic_views.urls'