1

I'm trying to save date formatted as dd/mm/yyyy from a view using Django to a postgresql table. I get the following error:

ValidationError: [u"'2014/01/30' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

how can i change the format of a specific table or the entire database?

I've tried:ALTER DATABASE myDb SET datestyle TO "ISO, YMD";

then when i run SHOW lc_ctype; i get:

lc_ctype   
-------------
en_US.UTF-8

and SELECT current_date; returns

  date    
------------
 2014-01-29

So it looks good, what raise the question if the error is in Django

my views.py looks like this:

logEntry, created = Logs.objects.get_or_create(entrydate=request_data['entryDate'], message=request_data['message'])
logEntry.save()

and my models.py

class Logs(models.Model):
   entrydate= models.DateTimeField(auto_now_add=True)
   message = models.CharField(max_length=100)

2 Answers 2

1

Rather than making changes on the db, I would recommend changing your view so that your date is correctly parsed into a datetime. Additionally, saving it as a timezone aware datetime object can be done as follows:

# views
from django.utils import timezone
from datetime import datetime


entrydt = datetime.strptime(request_data['entryDate'], "%Y/%m/%d")
entrydt = entrydt.replace(tzinfo=timezone.get_current_timezone())
logEntry, created = Logs.objects.get_or_create(entrydate=entrydt, 
                                               message=request_data['message'])
logEntry.save()
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you have DateTimeField instead DateField in your model. This error message in DateTimeField Change DateTimeField -> DateField

1 Comment

if it's must in YYYY-MM-DD format then i think '2014/01/30'.replace('/','-'). But it's ugly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.