Refs #23656 -- Required FormMixin.get_form() form_class parameter to be optional.
Per deprecation timeline.
This commit is contained in:
parent
849037af36
commit
491de4f07c
|
@ -1,11 +1,6 @@
|
||||||
import inspect
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.forms import models as model_forms
|
from django.forms import models as model_forms
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.utils import six
|
|
||||||
from django.utils.deprecation import RemovedInDjango110Warning
|
|
||||||
from django.utils.encoding import force_text
|
from django.utils.encoding import force_text
|
||||||
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
|
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
|
||||||
from django.views.generic.detail import (
|
from django.views.generic.detail import (
|
||||||
|
@ -13,28 +8,7 @@ from django.views.generic.detail import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class FormMixinBase(type):
|
class FormMixin(ContextMixin):
|
||||||
def __new__(cls, name, bases, attrs):
|
|
||||||
get_form = attrs.get('get_form')
|
|
||||||
if get_form and inspect.isfunction(get_form):
|
|
||||||
try:
|
|
||||||
inspect.getcallargs(get_form, None)
|
|
||||||
except TypeError:
|
|
||||||
warnings.warn(
|
|
||||||
"`%s.%s.get_form` method must define a default value for "
|
|
||||||
"its `form_class` argument." % (attrs['__module__'], name),
|
|
||||||
RemovedInDjango110Warning, stacklevel=2
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_form_with_form_class(self, form_class=None):
|
|
||||||
if form_class is None:
|
|
||||||
form_class = self.get_form_class()
|
|
||||||
return get_form(self, form_class=form_class)
|
|
||||||
attrs['get_form'] = get_form_with_form_class
|
|
||||||
return super(FormMixinBase, cls).__new__(cls, name, bases, attrs)
|
|
||||||
|
|
||||||
|
|
||||||
class FormMixin(six.with_metaclass(FormMixinBase, ContextMixin)):
|
|
||||||
"""
|
"""
|
||||||
A mixin that provides a way to show and handle a form in a request.
|
A mixin that provides a way to show and handle a form in a request.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -59,10 +59,6 @@ FormMixin
|
||||||
:meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
|
:meth:`~django.views.generic.edit.FormMixin.get_form_kwargs`.
|
||||||
If ``form_class`` isn't provided :meth:`get_form_class` will be used.
|
If ``form_class`` isn't provided :meth:`get_form_class` will be used.
|
||||||
|
|
||||||
.. versionchanged:: 1.8
|
|
||||||
|
|
||||||
The ``form_class`` argument is not required anymore.
|
|
||||||
|
|
||||||
.. method:: get_form_kwargs()
|
.. method:: get_form_kwargs()
|
||||||
|
|
||||||
Build the keyword arguments required to instantiate the form.
|
Build the keyword arguments required to instantiate the form.
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.test import SimpleTestCase, TestCase, override_settings
|
from django.test import SimpleTestCase, TestCase, override_settings
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
from django.utils.deprecation import RemovedInDjango110Warning
|
|
||||||
from django.views.generic.base import View
|
from django.views.generic.base import View
|
||||||
from django.views.generic.edit import CreateView, FormMixin, ModelFormMixin
|
from django.views.generic.edit import CreateView, FormMixin, ModelFormMixin
|
||||||
|
|
||||||
|
@ -59,28 +56,6 @@ class FormMixinTests(SimpleTestCase):
|
||||||
'get_form() should fallback to get_form_class() if none is provided.'
|
'get_form() should fallback to get_form_class() if none is provided.'
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_get_form_missing_form_class_default_value(self):
|
|
||||||
with warnings.catch_warnings(record=True) as w:
|
|
||||||
warnings.filterwarnings('always')
|
|
||||||
|
|
||||||
class MissingDefaultValue(FormMixin):
|
|
||||||
request = RequestFactory().get('/')
|
|
||||||
form_class = forms.Form
|
|
||||||
|
|
||||||
def get_form(self, form_class):
|
|
||||||
return form_class(**self.get_form_kwargs())
|
|
||||||
self.assertEqual(len(w), 1)
|
|
||||||
self.assertEqual(w[0].category, RemovedInDjango110Warning)
|
|
||||||
self.assertEqual(
|
|
||||||
str(w[0].message),
|
|
||||||
'`generic_views.test_edit.MissingDefaultValue.get_form` method '
|
|
||||||
'must define a default value for its `form_class` argument.'
|
|
||||||
)
|
|
||||||
|
|
||||||
self.assertIsInstance(
|
|
||||||
MissingDefaultValue().get_form(), forms.Form,
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_get_context_data(self):
|
def test_get_context_data(self):
|
||||||
class FormContext(FormMixin):
|
class FormContext(FormMixin):
|
||||||
request = RequestFactory().get('/')
|
request = RequestFactory().get('/')
|
||||||
|
|
Loading…
Reference in New Issue