Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving DateTimeField with mongoengine

Using the Django framework (1.3.1), together with Mongoengine.

When trying to save a posted field (the due date), it bails out with a

ValidationError (cannot parse date "2013-12-31": ['DueDate'])

However when saving the date via datetime.datetime.now() it works fine. After searching for examples, I'm out of options.

The related parts of my code (with a normal HTML form using the text input tag):

views.py

goal.DueDate = request.POST['duedate']
goal.save()

models.py

class Goal(Document):
    DueDate = DateTimeField()
    last_update = DateTimeField(required=True)

Any idea?

Update (can't answer myself yet):

Ok.. found the solution. Typing it, apparently gave new insights.

goal.DueDate = datetime.datetime.strptime(request.POST['duedate'], '%Y-%m-%d')

like image 892
user2391564 Avatar asked Oct 22 '22 09:10

user2391564


1 Answers

DateTimeField expects a datetime, not a string.
If the format is well known, you can use strptime like in your update, or dateutil parse method which is able to guess format.

You should also think about adopting a safer ISO formatted string send in the form from the web side.

like image 83
Léo Avatar answered Oct 27 '22 21:10

Léo