Saving DateTimeField with mongoengine

Using the Django framework (1.3.1) with Mongoengine.

When you try to save a published field (due date), it is issued using

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

However, when saving the date using datetime.datetime.now() it works fine. After searching for examples, I have no options.

Related parts of my code (with a regular HTML form using a 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?

Refresh (can't answer yet):

Ok, found a solution. Typing seems to have given new ideas.

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

+4
source share
1 answer

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

You should also consider using a more secure ISO formatted string in the form from the web page.

+2
source

Source: https://habr.com/ru/post/1481256/


All Articles