Commits

Thomas Pelletier  committed d0bba48

modelform_metaclass.patch first try

  • Participants
  • Parent commits 6a08a41

Comments (0)

Files changed (2)

File modelform_metaclass.patch

+# HG changeset patch
+# Parent f4f68695f8e007ef70b438c5cf653bc43cd7f2ca
+
+diff -r f4f68695f8e0 django/forms/models.py
+--- a/django/forms/models.py	Fri Sep 02 03:47:49 2011 +0000
++++ b/django/forms/models.py	Sat Sep 03 18:13:36 2011 +0200
+@@ -12,7 +12,7 @@
+ from django.utils.translation import ugettext_lazy as _, ugettext
+ 
+ from util import ErrorList
+-from forms import BaseForm, get_declared_fields
++from forms import BaseForm, get_declared_fields, Form
+ from fields import Field, ChoiceField
+ from widgets import SelectMultiple, HiddenInput, MultipleHiddenInput
+ from widgets import media_property
+@@ -182,7 +182,8 @@
+         self.widgets = getattr(options, 'widgets', None)
+ 
+ 
+-class ModelFormMetaclass(type):
++from forms import DeclarativeFieldsMetaclass
++class ModelFormMetaclass(DeclarativeFieldsMetaclass):
+     def __new__(cls, name, bases, attrs):
+         formfield_callback = attrs.pop('formfield_callback', None)
+         try:
+@@ -191,6 +192,10 @@
+             # We are defining ModelForm itself.
+             parents = None
+         declared_fields = get_declared_fields(bases, attrs, False)
++
++        simple_bases = [b for b in bases if issubclass(b, Form)]
++        declared_fields.update(get_declared_fields(simple_bases, attrs))
++
+         new_class = super(ModelFormMetaclass, cls).__new__(cls, name, bases,
+                 attrs)
+         if not parents:
+diff -r f4f68695f8e0 tests/regressiontests/forms/tests/models.py
+--- a/tests/regressiontests/forms/tests/models.py	Fri Sep 02 03:47:49 2011 +0000
++++ b/tests/regressiontests/forms/tests/models.py	Sat Sep 03 18:13:36 2011 +0200
+@@ -193,3 +193,55 @@
+             model=A
+ 
+         self.assertTrue(issubclass(ModelFormMetaclass('Form', (ModelForm,), {'Meta': Meta}), ModelForm))
++
++from django.forms import EmailField
++from sets import Set
++class ModelFormInheritsStdForm(TestCase):
++
++    def setUp(self):
++        class AForm(Form):
++            inherited_field = EmailField(label='an email', max_length=75)
++
++        class AModel(models.Model):
++            a_field = models.CharField(max_length=255, default="foo")
++            b_field = models.CharField(max_length=255, default="foo")
++
++        self.AForm = AForm
++        self.AModel = AModel
++
++
++    def test_inheritance(self):
++
++        
++        class AModelForm(ModelForm):
++            class Meta:
++                model = self.AModel
++
++        class FinalForm(AModelForm, self.AForm):
++
++            def awesome_method(self):
++                return self.fields
++
++        final_instance = FinalForm()
++
++        self.assertTrue(hasattr(final_instance, 'awesome_method'))
++        self.assertEqual(Set(['a_field', 'b_field', 'inherited_field']),
++                         Set(final_instance.fields.keys()))
++
++    def test_custom_meta(self):
++
++        class AModelForm(ModelForm):
++            class Meta:
++                model = self.AModel
++                exclude = ('a_field',)
++
++        class FinalForm(AModelForm, self.AForm):
++
++            def awesome_method(self):
++                return self.fields
++
++        final_instance = FinalForm()
++
++        self.assertTrue(hasattr(final_instance, 'awesome_method'))
++        self.assertEqual(Set(['b_field', 'inherited_field']),
++                         Set(final_instance.fields.keys()))
+modelform_metaclass.patch
 # Placed by Bitbucket