1
0
mirror of https://github.com/django/django.git synced 2025-07-06 18:59:13 +00:00

[soc2009/model-validation] A few words on validation methods on models

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11439 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Honza Král 2009-08-12 20:11:30 +00:00
parent 2799e92843
commit 69fef1daef

View File

@ -27,6 +27,30 @@ The keyword arguments are simply the names of the fields you've defined on your
model. Note that instantiating a model in no way touches your database; for
that, you need to ``save()``.
Validating objects
==================
.. versionadded:: 1.2
To validate your model, just call it's ``clean()`` method:
.. method:: Model.clean([exclude=[]])
The optional ``exclude`` argument can contain a list of field names that should
be omitted when validating. This method raises ``ValidationError`` containing a
message dict with errors from all fields.
To add your own validation logic, override the supplied ``validate()`` method:
.. method:: Model.validate()
The ``validate()`` method on ``Model`` by default checks for uniqueness of
fields and group of fields that are declared to be unique so remember to call
``super.validate()`` if you want this validation to run.
Any ``ValidationError`` raised in this method will be propagated in the
``message_dict`` under ``NON_FIELD_ERRORS``.
Saving objects
==============