mirror of https://github.com/django/django.git
Fixed #14082 -- Use metaclass of provided ModelForm subclass in modelform_factory. Thanks jspiros and Stephen Burrows for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16334 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
64e19ffb4e
commit
684b0396cf
|
@ -396,7 +396,12 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
|
||||||
'formfield_callback': formfield_callback
|
'formfield_callback': formfield_callback
|
||||||
}
|
}
|
||||||
|
|
||||||
return ModelFormMetaclass(class_name, (form,), form_class_attrs)
|
form_metaclass = ModelFormMetaclass
|
||||||
|
|
||||||
|
if issubclass(form, BaseModelForm) and hasattr(form, '__metaclass__'):
|
||||||
|
form_metaclass = form.__metaclass__
|
||||||
|
|
||||||
|
return form_metaclass(class_name, (form,), form_class_attrs)
|
||||||
|
|
||||||
|
|
||||||
# ModelFormSets ##############################################################
|
# ModelFormSets ##############################################################
|
||||||
|
|
|
@ -4,7 +4,7 @@ from django import forms
|
||||||
from django.core.exceptions import FieldError, ValidationError
|
from django.core.exceptions import FieldError, ValidationError
|
||||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
from django.forms.models import (modelform_factory, ModelChoiceField,
|
from django.forms.models import (modelform_factory, ModelChoiceField,
|
||||||
fields_for_model, construct_instance)
|
fields_for_model, construct_instance, ModelFormMetaclass)
|
||||||
from django.utils import unittest
|
from django.utils import unittest
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
@ -460,3 +460,19 @@ class EmptyFieldsTestCase(TestCase):
|
||||||
self.assertTrue(form.is_valid())
|
self.assertTrue(form.is_valid())
|
||||||
instance = construct_instance(form, Person(), fields=())
|
instance = construct_instance(form, Person(), fields=())
|
||||||
self.assertEqual(instance.name, '')
|
self.assertEqual(instance.name, '')
|
||||||
|
|
||||||
|
|
||||||
|
class CustomMetaclass(ModelFormMetaclass):
|
||||||
|
def __new__(cls, name, bases, attrs):
|
||||||
|
new = super(CustomMetaclass, cls).__new__(cls, name, bases, attrs)
|
||||||
|
new.base_fields = {}
|
||||||
|
return new
|
||||||
|
|
||||||
|
class CustomMetaclassForm(forms.ModelForm):
|
||||||
|
__metaclass__ = CustomMetaclass
|
||||||
|
|
||||||
|
|
||||||
|
class CustomMetaclassTestCase(TestCase):
|
||||||
|
def test_modelform_factory_metaclass(self):
|
||||||
|
new_cls = modelform_factory(Person, form=CustomMetaclassForm)
|
||||||
|
self.assertEqual(new_cls.base_fields, {})
|
||||||
|
|
Loading…
Reference in New Issue