You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
When today's date is entered in the form it shows a form error
saying that this date (today) is in the future.
What is wrong with the validator I'm using to allow dates until
today?
models.py
...
import datetime
...
class SolicitudBase(models.Model):
....
fecha =
models.DateField(validators=[MaxValueValidator(datetime.date.today(),
message="This date can't be in the future")])
....
I'm using Mysql and the following settings in my django project
related to timezone are commented:
#TIME_ZONE = 'EST'
#USE_TZ = True
My server (debian 7) is using US/Eastern timezone.
and right now:
root@webapp:~# date
Wed Sep 23 17:04:36 EDT 2015
Thanks in advance,
Felix.
Simon Charette
unread,
Sep 23, 2015, 5:44:21 PM9/23/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
Hi Felix,
The way you create your validator instance it is passed the value returned by `date.today()` at module initialization time. That is when the Python process starts.
It means that if you started your development server or say a gunicorn process on your server yesterday the max value the value will compare against will be yesterday and not the actual date which is what I'm assuming you're trying to do here.
Instead of relying on MaxValueValidator I suggest you write your own by simply defining the following function and passing in your field validators list.
def validate_past_date(value): if value > datetime.date.today(): raise ValidationError('This date can't be in the future')
Cheers, Simon
felix
unread,
Sep 24, 2015, 3:13:01 PM9/24/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
El 23/09/15 17:12, felix escribió:
Thanks Simon for your suggestion.
I forgot to mention that it only happens with the present date
(today) but it doesn't happen with past dates. So it makes me think
it could be a time zone issue.
Cheers,
Felix.
Simon Charette
unread,
Sep 24, 2015, 4:18:28 PM9/24/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Django users
Hi Felix,
From what I know model and form DateField are completely TIME_ZONE agnostics.
This is not the case for DateTimeField however. Is this what you're using?
Simon
felix
unread,
Nov 2, 2015, 8:50:05 AM11/2/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to django...@googlegroups.com
El 24/09/15 15:18, felix escribió:
You were right Simon.
Using a custom validator instead of MaxValueValidator for dates
solved my issue.
....
def validate_not_future_date(value):
if value > datetime.date.today():
raise ValidationError('%s is in the future' % value)
....
class SolicitudBase(models.Model):
....
fecha = models.DateField(validators=[validate_not_future_date])