Topic guide Creating forms from models ModelFormValidation on a ModelForm
You can override the clean() method on a model form to provide additional validation in the same way you can on a normal form.
689 sections across all versions Narrow to Django 6.0 (current) →
Topic guide Creating forms from models ModelFormValidation on a ModelForm
You can override the clean() method on a model form to provide additional validation in the same way you can on a normal form.
Reference The Django admin site ModelAdmin objects
ModelAdmin.formfield_for_manytomany(db_field,request,**kwargs) : Like the formfield_for_foreignkey method, the formfield_for_manytomany method can be overridden to change the default formfield for a many to many field.
Reference PostgreSQL specific form fields and widgets Fields
classSplitArrayField(base_field,size,remove_trailing_nulls=False) : This field handles arrays by reproducing the underlying field a fixed number of times.
Reference Validators
A validator is a callable that takes a value and raises a ValidationError if it doesn’t meet some criteria. Validators can be useful for reusing validation logic between different types of fields.
Topic guide Working with forms Building a formBuilding a form in Django
We don’t need to do much in our name.html template: All the form’s fields and their attributes will be unpacked into HTML markup from that {{ form }} by Django’s template language.
Reference Model instance reference
… as a whole - Model.clean() Validate the field uniqueness - Model.validate_unique() Validate the constraints - Model.validate_constraints() All four steps are performed when you call a model’s full_clean() method. When you use a ModelForm, the call to is_valid() will perform these …
Django provides a rich framework to facilitate the creation of forms and the manipulation of form data.
Topic guide Working with forms Building a form
Suppose you want to create a simple form on your website, in order to obtain the user’s name.
Reference Widgets Built-in widgetsWidgets handling input of text
classPasswordInput[source] : - input_type: 'password' - template_name: 'django/forms/widgets/password.html' - Renders as: <input type="password" ...>
Reference Model field reference Field options
Field.choices[source] A mapping or iterable in the format described below to use as choices for this field.
Topic guide File Uploads Basic file uploads
If you want to upload multiple files using one form field, create a subclass of the field’s widget and set its allow_multiple_selected class attribute to True.
Reference The Forms API Outputting forms as HTML
classErrorList(initlist=None,error_class=None,renderer=None,field_id=None)[source] : By default, forms use django.forms.utils.ErrorList to format validation errors. ErrorList is a list like object where initlist is the list of errors. In addition this class has the following attributes and methods.
Reference Widgets Built-in widgetsWidgets handling input of text
New in Django 5.2. classTelInput[source] : - input_type: 'tel' - template_name: 'django/forms/widgets/tel.html' - Renders as: <input type="tel" ...>
Reference Model field reference Field types
The default form widget for this field is a TextInput. CharField has the following extra arguments: CharField.max_length : The maximum length (in characters) of the field. The max_length is enforced at the database level and in Django’s validation using MaxLengthValidator.
Topic guide Formsets Validating the number of forms in a formset
If validate_max=True is passed to formset_factory(), validation will also check that the number of forms in the data set, minus those marked for deletion, is less than or equal to max_num.
Topic guide Formsets Validating the number of forms in a formset
If validate_min=True is passed to formset_factory(), validation will also check that the number of forms in the data set, minus those marked for deletion, is greater than or equal to min_num.
Topic guide Creating forms from models ModelForm
Every ModelForm also has a save() method. This method creates and saves a database object from the data bound to the form.
Topic guide Creating forms from models Inline formsets
When overriding methods on InlineFormSet, you should subclass BaseInlineFormSet rather than BaseModelFormSet. For example, if you want to override clean(): See also Overriding clean() on a ModelFormSet. Then when you create your inline formset, pass in the optional argument formset:
Reference The Django admin site ModelAdmin objectsModelAdmin options
Use the following options to override the default templates used by the ModelAdmin views: ModelAdmin.add_form_template : Path to a custom template, used by add_view(). ModelAdmin.change_form_template : Path to a custom template, used by change_view().
Reference The Django admin site InlineModelAdmin objects
The shared features are: form fieldsets fields formfield_overrides exclude filter_horizontal filter_vertical ordering prepopulated_fields get_fieldsets() get_queryset() radio_fields readonly_fields view_on_site empty_value_display autocomplete_fields raw_id_fields formfield_for_choice_field() formfield_for_foreignkey() formfield_for_manytomany() has_module_permission() The InlineModelAdmin class adds or customizes: InlineModelAdmin.model : The model which the inline is using.