Changes between Version 37 and Version 38 of NewformsHOWTO


Ignore:
Timestamp:
06/24/2008 08:06:24 AM (7 years ago)
Author:
lukeplant
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • NewformsHOWTO

    v37 v38  
    526526== Q: How do I add custom validation? ==
    527527
    528 One option is to override ```get_form()``` in your ```ModelAdmin``` subclass and provide a custom form that does the validation.
     528One option is to override ```get_form()``` in your ```ModelAdmin``` subclass and provide a custom form that does the validation.  Alternatively, note that ```get_form()``` uses the class defined in the ```.form``` attribute of your ```ModelAdmin``` subclass, which by default is ``models.[http://www.djangoproject.com/documentation/modelforms/ ModelForm]``.  You can subclass this class and use it as below:
     529
     530{{{
     531#!python
     532class MyModelForm(models.ModelForm):
     533    def clean(self):
     534        pass # Your custom validation here
     535
     536class MyModelAdmin(admin.ModelAdmin):
     537    form = MyModelForm
     538}}}
    529539
    530540If you want custom validation for a field in the admin '''and''' newforms [http://www.djangoproject.com/documentation/modelforms/ ModelForm] instances you can subclass a field in your model and override ```formfield()```. You can then return the form field you like or a complete [http://www.djangoproject.com/documentation/newforms/#creating-custom-fields custom form field].
Back to Top