Django

Code

Changeset 8208

Show
Ignore:
Timestamp:
08/04/08 14:29:33 (5 months ago)
Author:
brosner
Message:

Added the missing form option to the ModelAdmin? options section. Also added a section for custom validation in the admin.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/admin.txt

    r8184 r8208  
    6969 
    7070    date_hierarchy = 'pub_date' 
     71 
     72``form`` 
     73~~~~~~~~ 
     74 
     75The default ``forms.ModelForm`` class used to generate the form on the 
     76add/change pages for models. You can easily change this to your own 
     77``ModelForm`` to override the default form behavior of the add/change pages. 
    7178 
    7279``fieldsets`` 
     
    529536.. _regular media definitions on forms: ../forms/#media 
    530537 
     538Adding custom validation to the admin 
     539------------------------------------- 
     540 
     541Adding custom validation of data in the admin is quite easy. The automatic 
     542admin interfaces reuses the Django `forms`_ module. The ``ModelAdmin`` class 
     543gives you the ability define your own form:: 
     544 
     545    class ArticleAdmin(admin.ModelAdmin): 
     546        form = MyArticleAdminForm 
     547 
     548``MyArticleAdminForm`` can be defined anywhere as long as you import where 
     549needed. Now within your form you can add your own custom validation for 
     550any field:: 
     551     
     552    class MyArticleAdminForm(forms.ModelForm): 
     553        def clean_name(self): 
     554            # do something that validates your data 
     555            return self.cleaned_data["name"] 
     556 
     557It is important you use a ``ModelForm`` here otherwise things can break. See 
     558the `forms`_ documentation on `custom validation`_ for more information. 
     559 
     560.. _forms: ../forms/ 
     561.. _custom validation: ../forms/#custom-form-and-field-validation 
     562 
    531563``InlineModelAdmin`` objects 
    532564============================