2009/11/12 Dirk Uys <dirkc...@gmail.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
>
> --

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

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=.


Reply via email to