Fixed #7973 -- Added exclude to BaseModelAdmin to make everything consistent with the form/formset factories. Refs #8071 to make it easier to get at exclude. Thanks julien for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8861 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
326b26656a
commit
c1de41f4d2
|
@ -31,6 +31,7 @@ class BaseModelAdmin(object):
|
||||||
"""Functionality common to both ModelAdmin and InlineAdmin."""
|
"""Functionality common to both ModelAdmin and InlineAdmin."""
|
||||||
raw_id_fields = ()
|
raw_id_fields = ()
|
||||||
fields = None
|
fields = None
|
||||||
|
exclude = None
|
||||||
fieldsets = None
|
fieldsets = None
|
||||||
form = forms.ModelForm
|
form = forms.ModelForm
|
||||||
filter_vertical = ()
|
filter_vertical = ()
|
||||||
|
@ -262,9 +263,14 @@ class ModelAdmin(BaseModelAdmin):
|
||||||
fields = flatten_fieldsets(self.declared_fieldsets)
|
fields = flatten_fieldsets(self.declared_fieldsets)
|
||||||
else:
|
else:
|
||||||
fields = None
|
fields = None
|
||||||
|
if self.exclude is None:
|
||||||
|
exclude = []
|
||||||
|
else:
|
||||||
|
exclude = self.exclude
|
||||||
defaults = {
|
defaults = {
|
||||||
"form": self.form,
|
"form": self.form,
|
||||||
"fields": fields,
|
"fields": fields,
|
||||||
|
"exclude": exclude + kwargs.get("exclude", []),
|
||||||
"formfield_callback": self.formfield_for_dbfield,
|
"formfield_callback": self.formfield_for_dbfield,
|
||||||
}
|
}
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
|
@ -780,11 +786,16 @@ class InlineModelAdmin(BaseModelAdmin):
|
||||||
fields = flatten_fieldsets(self.declared_fieldsets)
|
fields = flatten_fieldsets(self.declared_fieldsets)
|
||||||
else:
|
else:
|
||||||
fields = None
|
fields = None
|
||||||
|
if self.exclude is None:
|
||||||
|
exclude = []
|
||||||
|
else:
|
||||||
|
exclude = self.exclude
|
||||||
defaults = {
|
defaults = {
|
||||||
"form": self.form,
|
"form": self.form,
|
||||||
"formset": self.formset,
|
"formset": self.formset,
|
||||||
"fk_name": self.fk_name,
|
"fk_name": self.fk_name,
|
||||||
"fields": fields,
|
"fields": fields,
|
||||||
|
"exclude": exclude + kwargs.get("exclude", []),
|
||||||
"formfield_callback": self.formfield_for_dbfield,
|
"formfield_callback": self.formfield_for_dbfield,
|
||||||
"extra": self.extra,
|
"extra": self.extra,
|
||||||
"max_num": self.max_num,
|
"max_num": self.max_num,
|
||||||
|
|
|
@ -181,6 +181,32 @@ displayed, sequentially, in the form.
|
||||||
dictionary key that is within the ``fieldsets`` option, as described in
|
dictionary key that is within the ``fieldsets`` option, as described in
|
||||||
the previous section.
|
the previous section.
|
||||||
|
|
||||||
|
``exclude``
|
||||||
|
~~~~~~~~~~~
|
||||||
|
|
||||||
|
This attribute, if given, should be a list of field names to exclude from the
|
||||||
|
form.
|
||||||
|
|
||||||
|
For example, let's consider the following model::
|
||||||
|
|
||||||
|
class Author(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
title = models.CharField(max_length=3)
|
||||||
|
birth_date = models.DateField(blank=True, null=True)
|
||||||
|
|
||||||
|
If you want a form for the ``Author`` model that includes only the ``name``
|
||||||
|
and ``title`` fields, you would specify ``fields`` or ``exclude`` like this::
|
||||||
|
|
||||||
|
class AuthorAdmin(admin.ModelAdmin):
|
||||||
|
fields = ('name', 'title')
|
||||||
|
|
||||||
|
class AuthorAdmin(admin.ModelAdmin):
|
||||||
|
exclude = ('birth_date',)
|
||||||
|
|
||||||
|
Since the Author model only has three fields, ``name``, ``title``, and
|
||||||
|
``birth_date``, the forms resulting from the above declarations will contain
|
||||||
|
exactly the same fields.
|
||||||
|
|
||||||
``filter_horizontal``
|
``filter_horizontal``
|
||||||
~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -701,5 +701,4 @@ False
|
||||||
>>> formset.is_valid()
|
>>> formset.is_valid()
|
||||||
True
|
True
|
||||||
|
|
||||||
|
|
||||||
"""}
|
"""}
|
||||||
|
|
|
@ -116,6 +116,23 @@ displayed because you forgot to add it to fields/fielsets
|
||||||
['name']
|
['name']
|
||||||
|
|
||||||
|
|
||||||
|
# Using `exclude`.
|
||||||
|
|
||||||
|
>>> class BandAdmin(ModelAdmin):
|
||||||
|
... exclude = ['bio']
|
||||||
|
>>> ma = BandAdmin(Band, site)
|
||||||
|
>>> ma.get_form(request).base_fields.keys()
|
||||||
|
['name', 'sign_date']
|
||||||
|
|
||||||
|
# Using `fields` and `exclude`.
|
||||||
|
|
||||||
|
>>> class BandAdmin(ModelAdmin):
|
||||||
|
... fields = ['name', 'bio']
|
||||||
|
... exclude = ['bio']
|
||||||
|
>>> ma = BandAdmin(Band, site)
|
||||||
|
>>> ma.get_form(request).base_fields.keys()
|
||||||
|
['name']
|
||||||
|
|
||||||
If we specify a form, it should use it allowing custom validation to work
|
If we specify a form, it should use it allowing custom validation to work
|
||||||
properly. This won't, however, break any of the admin widgets or media.
|
properly. This won't, however, break any of the admin widgets or media.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue