TypeError: expected string or bytes-like object DateField

2

I'm having a very annoying problem in Django to work with date. I am sending from my angle a date that I select in a input data . In my view , I convert the date to this format: 2017-10-13

When I try to update in my DateField field, I get the error:

TypeError: expected string or bytes-like object

Field in Model:

data_resgate = models.DateField('Data Resgate', null=True, blank=True)

How do I do 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 thing is, when I give python manage.py shell to do the update manually, when I add the string 2017-10-13 , it saves correctly without error.

@ UPDATE2

When I just get the date received from input date , and I try to generate a date with datetime , it does not even convert because I get the date of the input in another way.

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
    
asked by anonymous 01.11.2017 / 15:17

1 answer

2

Since it was a bit difficult to know when you are getting this error, I try to help you by playing the same scenario (successfully) with a model called Test, as follows:

data = {'data_resgate': '2017-10-10'}
teste = Teste()
teste.data_resgate = datetime.datetime.strptime(data.get('data_resgate', None), "%Y-%m-%d").date()
teste.save()

Notice that I used only the % Y-% m-% d format. Since the field is only of date, I understand that it is not necessary to work with additional information related to hours and minutes or to worry about Time Zone. It is not even necessary to cast a str , since the field is a DateField . Also make sure that data.get ('data_resgate', None) returns an str because if it returns another data type or until None (which was defined as default in the absence of this key), the strptime function will also return an error.

If the error remains, post a more detailed stacktrace so that we can better identify the time and place where this error is occurring. I hope I have helped!

    
07.11.2017 / 03:30