Ticket #14082: patch_r16322.diff

File patch_r16322.diff, 2.0 KB (added by Stephen Burrows, 13 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index ecfdc92..b34f4d0 100644
    a b def modelform_factory(model, form=ModelForm, fields=None, exclude=None,  
    396396        'formfield_callback': formfield_callback
    397397    }
    398398
    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)
    400405
    401406
    402407# ModelFormSets ##############################################################
  • tests/regressiontests/model_forms_regress/tests.py

    diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py
    index f536001..9817858 100644
    a b from django import forms  
    44from django.core.exceptions import FieldError, ValidationError
    55from django.core.files.uploadedfile import SimpleUploadedFile
    66from django.forms.models import (modelform_factory, ModelChoiceField,
    7     fields_for_model, construct_instance)
     7    fields_for_model, construct_instance, ModelFormMetaclass)
    88from django.utils import unittest
    99from django.test import TestCase
    1010
    class EmptyFieldsTestCase(TestCase):  
    460460        self.assertTrue(form.is_valid())
    461461        instance = construct_instance(form, Person(), fields=())
    462462        self.assertEqual(instance.name, '')
     463
     464
     465class 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
     471class CustomMetaclassForm(forms.ModelForm):
     472    __metaclass__ = CustomMetaclass
     473
     474
     475class CustomMetaclassTestCase(TestCase):
     476    def test_modelform_factory_metaclass(self):
     477        new_cls = modelform_factory(Person, form=CustomMetaclassForm)
     478        self.assertEqual(new_cls.base_fields, {})
Back to Top