Model validation

14 views
Skip to first unread message

Dirk Uys

unread,
Nov 12, 2009, 6:52:20 AM11/12/09
to django...@googlegroups.com
I have a model with a from and to date:

class Event (models.Model):
    start_date = models.DateField()
    end_date = models.DateField()

In order to ensure start_date is always before end_date i can think of 3 options:

1. Use form validation iow in the clean method of an inherited form
2. Use the django.db.models.signals.pre_save signal
3. Create a custor field that takes an parameter of the date field wich must allways be greater than or less than

What is the easiest way to achieve this?

Regards
Dirk

rebus_

unread,
Nov 12, 2009, 7:23:43 AM11/12/09
to django...@googlegroups.com
2009/11/12 Dirk Uys <dirk...@gmail.com>:
> --

You can add custom clean methods in your form:

class Event (models.Model):
start_date = models.DateField()
end_date = models.DateField()

def clean(self):
start = self.cleaned_data.get('start_date', False)
end = self.cleaned_data.get('end_date', False)
if not start or not end:
raise forms.ValidationError('message')
if start > end:
raise forms.ValidationError('start is greater then end')
return self.cleaned_data

This is just an example code.

Using this approach validation errors will not be associated with and field.
You could write clean_start_date and clean_end_date methods which
would evaluate each field and all validation errors would be
associated with specific field.

To learn more about form validation read [1] or [2]:

[1] http://docs.djangoproject.com/en/dev/ref/forms/validation/
[2] http://code.djangoproject.com/browser/django/trunk/docs/ref/forms/validation.txt

Dirk Uys

unread,
Nov 12, 2009, 9:35:18 AM11/12/09
to django...@googlegroups.com
On Thu, Nov 12, 2009 at 2:23 PM, rebus_ <r.dav.lc@gmail.com> wrote:
 
You can add custom clean methods in your form:

class Event (models.Model):
   start_date = models.DateField()
   end_date = models.DateField()

  def clean(self):
      start = self.cleaned_data.get('start_date', False)
      end = self.cleaned_data.get('end_date', False)
      if not start or not end:
         raise forms.ValidationError('message')
      if start > end:
         raise forms.ValidationError('start is greater then end')
      return self.cleaned_data

This is just an example code.

Using this approach validation errors will not be associated with and field.
You could write clean_start_date and clean_end_date methods which
would evaluate each field and all validation errors would be
associated with specific field.

To learn more about form validation read [1] or [2]:

[1] http://docs.djangoproject.com/en/dev/ref/forms/validation/
[2] http://code.djangoproject.com/browser/django/trunk/docs/ref/forms/validation.txt


Thanks! This is actually what I have done. The only problem is that I do not always use forms to supply data (sometimes I import from CVS) and would like to avoid doing the validation at two different points.

For the time being, I guess this will do

Cheers
Dirk

rebus_

unread,
Nov 12, 2009, 9:52:24 AM11/12/09
to django...@googlegroups.com
2009/11/12 Dirk Uys <dirk...@gmail.com>:
Technically, i think you should be able iterate over dataset imported
from CVS and bound each data row to form, validate the form and then
use the cleaned_data to create an object of the Model and save it or
whatever. Not sure what performance impacts this would have, if any,
but i guess that would depend on the amount of data that needs
validation.

Take a look at [1] forms API for example.

Also you could factor out the clean code into standalone function for
example and then use that function to validate the given data both in
form and the place you use imported data.

But then again i am no guru on the subject, these are just my opinions.

[1] http://docs.djangoproject.com/en/dev/ref/forms/api/
Reply all
Reply to author
Forward
0 new messages