diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-04-22 15:48:51 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-04-22 15:48:51 +0000 |
| commit | 6c15b5db6014a7dadb0c237e689bdaed4761fb16 (patch) | |
| tree | 88ef48e22dee3f86b3ebd8982b5f839977187a4a /django/forms/models.py | |
| parent | 71233bcdf3c90098531901da4e380165ed0059d4 (diff) | |
Fixed #10208: `ModelAdmin` now respects the `exclude` and `field` atributes of custom `ModelForm`s. Thanks, Alex Gaynor.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10619 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms/models.py')
| -rw-r--r-- | django/forms/models.py | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index d40a1ee55e..626e727e3a 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -344,16 +344,34 @@ class ModelForm(BaseModelForm): def modelform_factory(model, form=ModelForm, fields=None, exclude=None, formfield_callback=lambda f: f.formfield()): - # HACK: we should be able to construct a ModelForm without creating - # and passing in a temporary inner class - class Meta: - pass - setattr(Meta, 'model', model) - setattr(Meta, 'fields', fields) - setattr(Meta, 'exclude', exclude) + # Create the inner Meta class. FIXME: ideally, we should be able to + # construct a ModelForm without creating and passing in a temporary + # inner class. + + # Build up a list of attributes that the Meta object will have. + attrs = {'model': model} + if fields is not None: + attrs['fields'] = fields + if exclude is not None: + attrs['exclude'] = exclude + + # If parent form class already has an inner Meta, the Meta we're + # creating needs to inherit from the parent's inner meta. + parent = (object,) + if hasattr(form, 'Meta'): + parent = (form.Meta, object) + Meta = type('Meta', parent, attrs) + + # Give this new form class a reasonable name. class_name = model.__name__ + 'Form' - return ModelFormMetaclass(class_name, (form,), {'Meta': Meta, - 'formfield_callback': formfield_callback}) + + # Class attributes for the new form class. + form_class_attrs = { + 'Meta': Meta, + 'formfield_callback': formfield_callback + } + + return ModelFormMetaclass(class_name, (form,), form_class_attrs) # ModelFormSets ############################################################## @@ -617,13 +635,6 @@ def inlineformset_factory(parent_model, model, form=ModelForm, # enforce a max_num=1 when the foreign key to the parent model is unique. if fk.unique: max_num = 1 - if fields is not None: - fields = list(fields) - fields.append(fk.name) - else: - # get all the fields for this model that will be generated. - fields = fields_for_model(model, fields, exclude, formfield_callback).keys() - fields.append(fk.name) kwargs = { 'form': form, 'formfield_callback': formfield_callback, |
