Author: carljm
Date: 2011-06-07 09:15:54 -0700 (Tue, 07 Jun 2011)
New Revision: 16334
Modified:
django/trunk/django/forms/models.py
django/trunk/tests/regressiontests/model_forms_regress/tests.py
Log:
Fixed #14082 -- Use metaclass of provided ModelForm subclass in
modelform_factory. Thanks jspiros and Stephen Burrows for the patch.
Modified: django/trunk/django/forms/models.py
===================================================================
--- django/trunk/django/forms/models.py 2011-06-07 16:11:25 UTC (rev 16333)
+++ django/trunk/django/forms/models.py 2011-06-07 16:15:54 UTC (rev 16334)
@@ -396,9 +396,14 @@
'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 ##############################################################
class BaseModelFormSet(BaseFormSet):
Modified: django/trunk/tests/regressiontests/model_forms_regress/tests.py
===================================================================
--- django/trunk/tests/regressiontests/model_forms_regress/tests.py
2011-06-07 16:11:25 UTC (rev 16333)
+++ django/trunk/tests/regressiontests/model_forms_regress/tests.py
2011-06-07 16:15:54 UTC (rev 16334)
@@ -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 @@
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, {})
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.