Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #14082 -- Use metaclass of provided ModelForm subclass in model…

…form_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
  • Loading branch information...
commit 684b0396cfff1c4343a83bd600db69af9188986d 1 parent 64e19ff
@carljm carljm authored
View
7 django/forms/models.py
@@ -396,7 +396,12 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
'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 ##############################################################
View
18 tests/regressiontests/model_forms_regress/tests.py
@@ -4,7 +4,7 @@
from django.core.exceptions import FieldError, ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
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.test import TestCase
@@ -460,3 +460,19 @@ def test_empty_fields_to_construct_instance(self):
self.assertTrue(form.is_valid())
instance = construct_instance(form, Person(), fields=())
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, {})
Please sign in to comment.
Something went wrong with that request. Please try again.