Browse Source

Fixed #21753 -- Raised exception when both `form_class` and `fields` are specified.

Berker Peksag 10 years ago
parent
commit
343162410f

+ 4 - 0
django/views/generic/edit.py

@@ -121,6 +121,10 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
         """
         Returns the form class to use in this view.
         """
+        if self.fields is not None and self.form_class:
+            raise ImproperlyConfigured(
+                "Specifying both 'fields' and 'form_class' is not permitted."
+            )
         if self.form_class:
             return self.form_class
         else:

+ 11 - 0
docs/ref/class-based-views/mixins-editing.txt

@@ -115,6 +115,17 @@ ModelFormMixin
     :attr:`~django.views.generic.detail.SingleObjectMixin.queryset` attributes,
     describing the type of object that the ``ModelForm`` is manipulating.
 
+    If you specify both the
+    :attr:`~django.views.generic.edit.ModelFormMixin.fields` and
+    :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
+    :exc:`~django.core.exceptions.ImproperlyConfigured` exception will be
+    raised.
+
+    .. versionchanged:: 1.8
+
+        Previously if both ``fields`` and ``form_class`` were specified,
+        ``fields`` was silently ignored.
+
     **Mixins**
 
     * :class:`django.views.generic.edit.FormMixin`

+ 5 - 0
docs/releases/1.8.txt

@@ -783,6 +783,11 @@ Miscellaneous
   ``<WSGIRequest: GET '/somepath/'>``). This won't change the behavior of
   the :class:`~django.views.debug.SafeExceptionReporterFilter` class.
 
+* Class-based views that use :class:`~django.views.generic.edit.ModelFormMixin`
+  will raise an :exc:`~django.core.exceptions.ImproperlyConfigured` exception
+  when both the ``fields`` and ``form_class`` attributes are specified.
+  Previously, ``fields`` was silently ignored.
+
 .. _deprecated-features-1.8:
 
 Features deprecated in 1.8

+ 9 - 0
docs/topics/class-based-views/generic-editing.txt

@@ -139,11 +139,20 @@ inner ``Meta`` class on :class:`~django.forms.ModelForm`. Unless you define the
 form class in another way, the attribute is required and the view will raise
 an :exc:`~django.core.exceptions.ImproperlyConfigured` exception if it's not.
 
+If you specify both the :attr:`~django.views.generic.edit.ModelFormMixin.fields`
+and :attr:`~django.views.generic.edit.FormMixin.form_class` attributes, an
+:exc:`~django.core.exceptions.ImproperlyConfigured` exception will be raised.
+
 .. versionchanged:: 1.8
 
     Omitting the ``fields`` attribute was previously allowed and resulted in a
     form with all of the model's fields.
 
+.. versionchanged:: 1.8
+
+    Previously if both ``fields`` and ``form_class`` were specified,
+    ``fields`` was silently ignored.
+
 Finally, we hook these new views into the URLconf:
 
 .. snippet::

+ 11 - 0
tests/generic_views/test_edit.py

@@ -14,6 +14,7 @@ from django.views.generic.edit import FormMixin, ModelFormMixin, CreateView
 
 from . import views
 from .models import Artist, Author
+from .test_forms import AuthorForm
 
 
 class FormMixinTests(TestCase):
@@ -206,6 +207,16 @@ class CreateViewTests(TestCase):
         with self.assertRaisesMessage(ImproperlyConfigured, message):
             MyCreateView().get_form_class()
 
+    def test_define_both_fields_and_form_class(self):
+        class MyCreateView(CreateView):
+            model = Author
+            form_class = AuthorForm
+            fields = ['name']
+
+        message = "Specifying both 'fields' and 'form_class' is not permitted."
+        with self.assertRaisesMessage(ImproperlyConfigured, message):
+            MyCreateView().get_form_class()
+
 
 @override_settings(ROOT_URLCONF='generic_views.urls')
 class UpdateViewTests(TestCase):