views:

1197

answers:

3

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?

+1  A: 

In general, you should be able to handle what you want through the built in field types and their options or the model's meta options. You can also override the save method to perform validation/sanitation. If that is not sufficient, you can create your own field type.

The problem is that there is no good expected behavior. What should happen? Should an exception be raised? The fields are only really an abstraction at the database level, so there shouldn't be more information in there than what the database needs to know.

wbyoung
+8  A: 

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).

zgoda
+3  A: 

Model validation will be available shortly in Django version 1.2. It is available right now if you use a current Django svn checkout of the trunk.

Various clean methods are now available. See http://docs.djangoproject.com/en/dev/ref/models/instances/#id1 for details.

chefsmart