Anonymous avatar Anonymous committed e70be06

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.

Comments (0)

Files changed (4)

django/contrib/admin/options.py

     """Functionality common to both ModelAdmin and InlineAdmin."""
     raw_id_fields = ()
     fields = None
+    exclude = None
     fieldsets = None
     form = forms.ModelForm
     filter_vertical = ()
             fields = flatten_fieldsets(self.declared_fieldsets)
         else:
             fields = None
+        if self.exclude is None:
+            exclude = []
+        else:
+            exclude = self.exclude
         defaults = {
             "form": self.form,
             "fields": fields,
+            "exclude": exclude + kwargs.get("exclude", []),
             "formfield_callback": self.formfield_for_dbfield,
         }
         defaults.update(kwargs)
             fields = flatten_fieldsets(self.declared_fieldsets)
         else:
             fields = None
+        if self.exclude is None:
+            exclude = []
+        else:
+            exclude = self.exclude
         defaults = {
             "form": self.form,
             "formset": self.formset,
             "fk_name": self.fk_name,
             "fields": fields,
+            "exclude": exclude + kwargs.get("exclude", []),
             "formfield_callback": self.formfield_for_dbfield,
             "extra": self.extra,
             "max_num": self.max_num,

docs/ref/contrib/admin.txt

     dictionary key that is within the ``fieldsets`` option, as described in
     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``
 ~~~~~~~~~~~~~~~~~~~~~
 

tests/modeltests/model_formsets/models.py

 >>> formset.is_valid()
 True
 
-
 """}

tests/regressiontests/modeladmin/models.py

 ['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
 properly. This won't, however, break any of the admin widgets or media.
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.