Ticket #7973: 7973.exclude.diff

File 7973.exclude.diff, 3.4 KB (added by Julien Phalip, 16 years ago)

patch + doc + tests

  • django/django/contrib/admin/options.py

     
    116116    raw_id_fields = ()
    117117    fields = None
    118118    fieldsets = None
     119    exclude = None
    119120    form = forms.ModelForm
    120121    filter_vertical = ()
    121122    filter_horizontal = ()
     
    319320            fields = flatten_fieldsets(self.declared_fieldsets)
    320321        else:
    321322            fields = None
    322         return modelform_factory(self.model, form=self.form, fields=fields, formfield_callback=self.formfield_for_dbfield)
     323        return modelform_factory(self.model, form=self.form, fields=fields, formfield_callback=self.formfield_for_dbfield, exclude=self.exclude)
    323324
    324325    def get_formsets(self, request, obj=None):
    325326        for inline in self.inline_instances:
     
    726727        return inlineformset_factory(self.parent_model, self.model,
    727728            form=self.form, formset=self.formset, fk_name=self.fk_name,
    728729            fields=fields, formfield_callback=self.formfield_for_dbfield,
    729             extra=self.extra, max_num=self.max_num)
     730            extra=self.extra, max_num=self.max_num, exclude=self.exclude)
    730731
    731732    def get_fieldsets(self, request, obj=None):
    732733        if self.declared_fieldsets:
  • django/tests/regressiontests/modeladmin/models.py

     
    162162
    163163>>> band2.delete()
    164164
     165
     166Using 'exclude'.
     167>>> class BandAdmin(ModelAdmin):
     168...     exclude = ['bio']
     169>>> ma = BandAdmin(Band, site)
     170>>> ma.get_form(request).base_fields.keys()
     171['name', 'sign_date']
     172
     173Using 'fields' *and* 'exclude'.
     174>>> class BandAdmin(ModelAdmin):
     175...     fields = ['name', 'bio']
     176...     exclude = ['bio']
     177>>> ma = BandAdmin(Band, site)
     178>>> ma.get_form(request).base_fields.keys()
     179['name']
     180
     181
    165182# radio_fields behavior ################################################
    166183
    167184First, without any radio_fields specified, the widgets for ForeignKey
  • django/docs/admin.txt

     
    147147    ``django.utils.html.escape()`` to escape any HTML special
    148148    characters.
    149149
     150``exclude``
     151~~~~~~~~~~~
     152
     153This attribute, if given, should be a list of field names to exclude from the form.
     154
     155For example, let's consider the following model::
     156
     157    class Author(models.Model):
     158        name = models.CharField(max_length=100)
     159        title = models.CharField(max_length=3)
     160        birth_date = models.DateField(blank=True, null=True)
     161       
     162If you want a form for the ``Author`` model that includes only the ``name`` and ``title``
     163fields, you would specify ``fields`` or ``exclude`` like this::
     164
     165    class AuthorAdmin(admin.ModelAdmin):
     166        fields = ('name', 'title')
     167
     168    class AuthorAdmin(admin.ModelAdmin):
     169        exclude = ('birth_date',)
     170
     171Since the Author model only has 3 fields, 'name', 'title', and 'birth_date', the forms
     172resulting from the above declarations will contain exactly the same fields.
     173
    150174``filter_horizontal``
    151175~~~~~~~~~~~~~~~~~~~~~
    152176
Back to Top