Daniel Rosenthal
Daniel Rosenthal

Reputation: 1386

Django form custom Date validation

I have a django class that has two date attributes--a start date, and an end date. I want to make a custom validation requirement that says that the start date HAS to be before the end date (or on the same date). I know this revolves custom validation, but don't really know where this should go (in the model? in the view?, etc.).

Any advice? Thanks.

Upvotes: 0

Views: 1016

Answers (2)

Brent Washburne
Brent Washburne

Reputation: 13148

I would put the validation in both the model and the template. Yes, that's twice the work in two different languages (Python and Javascript), but you'll get cleaner data this way. Here's one way to do this in jQuery: end date greater than start date - jquery validation.

The model is the place to put all data validation for that model. Create a method that takes all the parameters and raises exceptions (ValueError, etc.) if the data is incorrect.

Upvotes: 0

Leandro
Leandro

Reputation: 2247

Add a Form validation.

If you want to validate on model save, use django pre_save signal

Upvotes: 1

Related Questions