From 75a96f06e916dda9124da9ddf765f3b8d5530914 Mon Sep 17 00:00:00 2001 From: Ryan Kaskel Date: Sun, 9 Feb 2014 10:53:23 +0000 Subject: [PATCH] Fixed #21967: Added check for object in ModelFormMixin.get_form_kwargs. Thanks lagovas.lagovas at gmail.com for the report. --- django/views/generic/edit.py | 3 ++- tests/generic_views/test_edit.py | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py index 0a8301f0db..84d9eb2fee 100644 --- a/django/views/generic/edit.py +++ b/django/views/generic/edit.py @@ -122,7 +122,8 @@ class ModelFormMixin(FormMixin, SingleObjectMixin): Returns the keyword arguments for instantiating the form. """ kwargs = super(ModelFormMixin, self).get_form_kwargs() - kwargs.update({'instance': self.object}) + if hasattr(self, 'object'): + kwargs.update({'instance': self.object}) return kwargs def get_success_url(self): diff --git a/tests/generic_views/test_edit.py b/tests/generic_views/test_edit.py index be26a14adf..92732c4f5f 100644 --- a/tests/generic_views/test_edit.py +++ b/tests/generic_views/test_edit.py @@ -9,7 +9,7 @@ from django import forms from django.test import TestCase from django.test.client import RequestFactory 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 .models import Artist, Author @@ -55,6 +55,12 @@ class ModelFormMixinTests(TestCase): form_class = views.AuthorGetQuerySetFormView().get_form_class() 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): urls = 'generic_views.urls'