Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Django model validation handled just through the forms API?

Is this the only way to create custom model validation? To do it using the forms? What if I want to send data to the database through means other than forms?

like image 441
rick Avatar asked Apr 30 '09 01:04

rick


People also ask

How does Django validate data?

Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What is form API in Django?

A Form instance is either bound to a set of data, or unbound. If it's bound to a set of data, it's capable of validating that data and rendering the form as HTML with the data displayed in the HTML.

Which method has a form instance which runs validation routines for all its fields?

The run_validators() method on a Field runs all of the field's validators and aggregates all the errors into a single ValidationError .


1 Answers

Currently Django does not provide any model-level validation (besides basic "NOT NULL", "UNIQUE" and length validations). This is on the TODO list but most likely will not fit upcoming 1.1 release.

You can perform validation related tasks in save() method of your model or use before_save signal (raising exception in signal handler will cause the transaction to be rolled back).

like image 82
zgoda Avatar answered Sep 30 '22 02:09

zgoda