Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Carl Meyer authored June 07, 2011
7  django/forms/models.py
@@ -396,7 +396,12 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
396 396
         'formfield_callback': formfield_callback
397 397
     }
398 398
 
399  
-    return ModelFormMetaclass(class_name, (form,), form_class_attrs)
  399
+    form_metaclass = ModelFormMetaclass
  400
+
  401
+    if issubclass(form, BaseModelForm) and hasattr(form, '__metaclass__'):
  402
+        form_metaclass = form.__metaclass__
  403
+
  404
+    return form_metaclass(class_name, (form,), form_class_attrs)
400 405
 
401 406
 
402 407
 # ModelFormSets ##############################################################
18  tests/regressiontests/model_forms_regress/tests.py
@@ -4,7 +4,7 @@
4 4
 from django.core.exceptions import FieldError, ValidationError
5 5
 from django.core.files.uploadedfile import SimpleUploadedFile
6 6
 from django.forms.models import (modelform_factory, ModelChoiceField,
7  
-    fields_for_model, construct_instance)
  7
+    fields_for_model, construct_instance, ModelFormMetaclass)
8 8
 from django.utils import unittest
9 9
 from django.test import TestCase
10 10
 
@@ -460,3 +460,19 @@ def test_empty_fields_to_construct_instance(self):
460 460
         self.assertTrue(form.is_valid())
461 461
         instance = construct_instance(form, Person(), fields=())
462 462
         self.assertEqual(instance.name, '')
  463
+
  464
+
  465
+class CustomMetaclass(ModelFormMetaclass):
  466
+    def __new__(cls, name, bases, attrs):
  467
+        new = super(CustomMetaclass, cls).__new__(cls, name, bases, attrs)
  468
+        new.base_fields = {}
  469
+        return new
  470
+
  471
+class CustomMetaclassForm(forms.ModelForm):
  472
+    __metaclass__ = CustomMetaclass
  473
+
  474
+
  475
+class CustomMetaclassTestCase(TestCase):
  476
+    def test_modelform_factory_metaclass(self):
  477
+        new_cls = modelform_factory(Person, form=CustomMetaclassForm)
  478
+        self.assertEqual(new_cls.base_fields, {})

0 notes on commit 684b039

Please sign in to comment.
Something went wrong with that request. Please try again.