On May 27, 5:56 am, Diogo Baeder <diogobae...@gmail.com> wrote:
> Hi,
>
> I've got two fields, in a model object: "from_date" and "to_date". How
> can I make the admin controller or model validate if the "from_date"
> field has always a lower value than "to_date"? Both are
> "django.db.models.DateField", but how can I create a validation between
> them?
>
> Thanks!

There are at least two answers to this questions :)

First you can add this logic in your model's save method, and raise a
ValueError if incoherent data gets there.

def save(self, force_insert=False, force_update=False):
  if self.to_date<self.from_date:
    raise ValueError("You shouldn't get here with these data. Please
contact someone!")
  super(MyModel, self).save(force_insert, force_update)

Second, clearly, incoherent data should never get to the model level,
but should be traped at form validation. So you should write a form
that implements a clean method where you can check the dates. This
holds true even if you submit the data only over the admin interface!

V
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to