Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Add past/present/future validations for Date and DateTime types

443 views
Skip to first unread message

Mariano Baragiola

unread,
Aug 30, 2016, 4:20:36 PM8/30/16
to django-d...@googlegroups.com
Hello django-developers@, 

On more than one project, I find myself implementing the same feature over
and over again. It's not a rare case scenario, for instance: it's common
that invoices have a date that belongs to the past or the present time, but
not the future.

Seeing that we have django.core.validators.MinValueValidator and
django.core.validators.MaxValueValidator, I propose the following:

* django.core.validators.DateIsPresent
* django.core.validators.DateIsPresentOrPast
* django.core.validators.DateIsPresentOrFuture
* django.core.validators.DateIsFuture

This way, one may easily use them at models.py just like MinValueValidator
and MaxValueValidator.

The code would be really simple, I have them -differently- implemented at:
https://github.com/heimdalerp/heimdalerp/blob/master/common/validators.py

Please, let me know if there's already an easier way to do this with
current Django, or if this belongs to a custom app instead.

If it is ok to add this feature to Django, I can gladly take care of it.

Thanks in advance. 

Tim Graham

unread,
Aug 30, 2016, 9:04:04 PM8/30/16
to Django developers (Contributions to Django itself), mariano....@yahoo.com
Thanks for the proposal. MaxValueValidator and MaxValueValidator are included because they're needed for IntegerField validation. I don't think the proposed validators meet the criteria of "needed in 80% of Django projects" or "can't easily be implemented as a third-party app".

Ares Ou

unread,
Aug 30, 2016, 10:41:42 PM8/30/16
to django-d...@googlegroups.com, mariano....@yahoo.com
Actually I remember you can use field__lt to compare with a datetime object (e.g. datetime.now()) and validate it.



Best regards,
Ares Ou

Software Engineer / Full-Stack Python Developer  |  Phone: (510) 328 - 5968


Ares Ou

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/62bd473f-19bb-4140-b04f-ba733e8b78e5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jani Tiainen

unread,
Aug 31, 2016, 3:31:11 AM8/31/16
to django-d...@googlegroups.com

Hi,

As Tim pointed out this can easily live as an external package.

Also you have to be really careful with dates and specially datetimes when playing with timezones and daylight saving time. Specially since you mention that invoice dates that may belong to past/present/future.

Thanks in advance.  --
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
Jani Tiainen

Josh Smeaton

unread,
Aug 31, 2016, 7:59:24 PM8/31/16
to Django developers (Contributions to Django itself)
Jani is correct, you'll need to be careful to handle timezones if timezones are activated in SETTINGS. I can see value in a django-validators external package that you could expand upon with newer validators as they come up. But whether or not you want to build and maintain such a project is obviously up to you. I agree with Tim, that this feature would probably be better off as an external package, at least for the moment.

Thanks!

Aymeric Augustin

unread,
Sep 5, 2016, 9:08:14 AM9/5/16
to django-d...@googlegroups.com, mariano....@yahoo.com
I suppose Min / MaxValueValidator cannot be reused here because they expect a fixed bound, while past / future depends on the current date?

The implementation Mariano provided looks simple enough to reproduce and adapt to each project’s requirements (even though I’d prefer duck typing to type checks). Note that it assumes that USE_TZ = False while Django sets USE_TZ = True by default.

-- 
Aymeric.

-- 
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
Reply all
Reply to author
Forward
0 new messages