2
I am having a very boring problem in Django to work with date. I am sending from my angular a date I select in a input data
. In my view
, i turn the date to that format: 2017-10-13
When I try to update my Datefield field, I get the error:
TypeError: expected string or bytes-like object
Field in the Model:
data_resgate = models.DateField('Data Resgate', null=True, blank=True)
How I’m doing the update:
model.data_resgate = str(datetime.datetime.strptime(data.get('data_resgate', None), "%Y-%m-%dT%H:%M:%S.000Z").date())
I’m with USE_L10N = True
.
@UPDATE
The worst is, when I give one python manage.py shell
to update manually, when I add the string 2017-10-13
, it saves correctly without giving error.
@UPDATE2
When I just pick up the date received from input date
, and try to generate a date with the datetime
, it doesn’t even convert because I receive the input date otherwise.
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 365, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: T00:00:00.000Z
The error gives in what called same methods?
– Jefferson Quesado
He’s giving in to
save()
. Pretty strange right?– Guilherme IA
And if you do:
model.data_resgate = str(datetime.datetime.strptime(str(data.get('data_resgate', None)), "%Y-%m-%dT%H:%M:%S.000Z").date())
?– Leonardo Pessoa
I tried, but it didn’t work. I’m putting up a string even '2017-10-13' and it’s not rolling. I think it has something to do with
USE_L10N
, or I must be sinning in some detail.– Guilherme IA