diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-04-22 16:14:02 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-04-22 16:14:02 +0000 |
| commit | 421b22e8eed0094669f6b197ac52bace5b5f29d9 (patch) | |
| tree | 716c4b5579988bb93ebf74ff90fd61b0aaced7ef /django/forms | |
| parent | cb92893598a31bf3af697d65e257a33f6686d6a2 (diff) | |
[1.0.X] Fixed #10208: `ModelAdmin` now respects the `exclude` and `field` atributes of custom `ModelForm`s. Thanks, Alex Gaynor. Backport of r10619 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10620 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms')
| -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 d2ca294cce..aa5f6ad795 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -331,16 +331,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 ############################################################## @@ -578,13 +596,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, |
