Django

Code

Changeset 8861

Show
Ignore:
Timestamp:
09/02/08 12:26:24 (4 months ago)
Author:
brosner
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/admin/options.py

    r8773 r8861  
    3232    raw_id_fields = () 
    3333    fields = None 
     34    exclude = None 
    3435    fieldsets = None 
    3536    form = forms.ModelForm 
     
    263264        else: 
    264265            fields = None 
     266        if self.exclude is None: 
     267            exclude = [] 
     268        else: 
     269            exclude = self.exclude 
    265270        defaults = { 
    266271            "form": self.form, 
    267272            "fields": fields, 
     273            "exclude": exclude + kwargs.get("exclude", []), 
    268274            "formfield_callback": self.formfield_for_dbfield, 
    269275        } 
     
    781787        else: 
    782788            fields = None 
     789        if self.exclude is None: 
     790            exclude = [] 
     791        else: 
     792            exclude = self.exclude 
    783793        defaults = { 
    784794            "form": self.form, 
     
    786796            "fk_name": self.fk_name, 
    787797            "fields": fields, 
     798            "exclude": exclude + kwargs.get("exclude", []), 
    788799            "formfield_callback": self.formfield_for_dbfield, 
    789800            "extra": self.extra, 
  • django/trunk/docs/ref/contrib/admin.txt

    r8858 r8861  
    181181    dictionary key that is within the ``fieldsets`` option, as described in 
    182182    the previous section. 
     183 
     184``exclude`` 
     185~~~~~~~~~~~ 
     186 
     187This attribute, if given, should be a list of field names to exclude from the 
     188form. 
     189 
     190For example, let's consider the following model:: 
     191 
     192    class Author(models.Model): 
     193        name = models.CharField(max_length=100) 
     194        title = models.CharField(max_length=3) 
     195        birth_date = models.DateField(blank=True, null=True) 
     196 
     197If you want a form for the ``Author`` model that includes only the ``name`` 
     198and ``title`` fields, you would specify ``fields`` or ``exclude`` like this:: 
     199     
     200    class AuthorAdmin(admin.ModelAdmin): 
     201        fields = ('name', 'title') 
     202     
     203    class AuthorAdmin(admin.ModelAdmin): 
     204        exclude = ('birth_date',) 
     205 
     206Since the Author model only has three fields, ``name``, ``title``, and 
     207``birth_date``, the forms resulting from the above declarations will contain 
     208exactly the same fields. 
    183209 
    184210``filter_horizontal`` 
  • django/trunk/tests/modeltests/model_formsets/models.py

    r8816 r8861  
    702702True 
    703703 
    704  
    705704"""} 
  • django/trunk/tests/regressiontests/modeladmin/models.py

    r8604 r8861  
    117117 
    118118 
     119# Using `exclude`. 
     120 
     121>>> class BandAdmin(ModelAdmin):  
     122...     exclude = ['bio']  
     123>>> ma = BandAdmin(Band, site)  
     124>>> ma.get_form(request).base_fields.keys()  
     125['name', 'sign_date'] 
     126  
     127# Using `fields` and `exclude`. 
     128 
     129>>> class BandAdmin(ModelAdmin):  
     130...     fields = ['name', 'bio']  
     131...     exclude = ['bio']  
     132>>> ma = BandAdmin(Band, site)  
     133>>> ma.get_form(request).base_fields.keys()  
     134['name'] 
     135 
    119136If we specify a form, it should use it allowing custom validation to work 
    120137properly. This won't, however, break any of the admin widgets or media.