Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 26, 2011 23:01:05

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Custom validation in model


Hi,
Supose that I have a model with a from and to date:

class Foo (models.Model):
from_date = models.DateField()
to_date = models.DateField()

What is the correct way to ensure that from_date is always before
to_date?
For now, I'm using only the admin site.

One way, may be, is to use custom validation to the admin, like
suggest inhttp://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin.

But i think that is better to have a method to validate, inside model
logic, as suggested in an old threadhttp://groups.google.com/group/django-users/browse_thread/thread/58c20c72f8b8725a/ec2a26c6118183fd?lnk=gst&q=validation#ec2a26c6118183fdThe latter one, does not work for me.... i dont know what i'm doing
wrong, i got an error msg :
'Foo' object has no attribute 'cleaned_data'

Regards,
Mario.

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 26, 2011 23:08:34

Shawn M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Custom validation in model


Overriding the clean method of the modelform is the way to go.

If you're getting an error about not having 'cleaned_data,' it could be because
your custom clean method doesn't return anything -- you're responsible for
returning it if you override it that method.

Shawn


--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support