Typeerror: expected string or bytes-like Object Datefield

Asked

Viewed 3,528 times

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?

  • 1

    He’s giving in to save(). Pretty strange right?

  • 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())?

  • 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.

1 answer

2


As it became a little difficult to know at what point you are receiving this error, I try to help you reproduce 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()

Note that I only used the format %Y-%m-%d. Since the field is date only, I understand that it is not necessary to work with additional information related to hours and minutes or to worry about Time Zone. You don’t even need to cast a cast for str, since the field is a Datefield. Make sure, moreover, that data.get('data_rescue', None) returns a str because, if it returns another type of data or up to None (which was defined as default in case of absence of this key), the function strptime 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’ve helped!

  • Thanks for the reply Eduardo. I edited as requested, but this is not how I receive the date of input date. She comes this way: 2017-11-18T00:00:00.000Z

  • Curious...I did the same previous test putting 2017-11-18T00:00:00.000Z in dictionary and %Y-%m-%Dt%H:%M:%S.000Z in format and it worked perfectly... The error message you receive says rightly that it only managed to parse to date...from the time it found nothing else. Are you sure the value that’s coming from the dictionary is correct? Checks if maybe the string has no hidden special characters, such as r or n, for example (among others). This dictionary is taking data from where?

  • Absolute certainty.. As you can see in the question, I have already tried to insert a manual date, and nothing. I do not understand why it is not working.

  • The Forms work perfectly.

  • He gives this warning on save(), however, if I remove the update from the data field, the rest works. Then really the error is in this data update.

  • I was intrigued by the fact that it worked here. Can we have any questions related to the versions of python and Django? If you are interested and want to email me small snippets of your model, your view and your template so I can simulate here and help you, I am available. If you think it’s viable, of course.

  • So, hacking into the code is tricky, but I can get you exactly what I’m doing. Briefly: from a <input type="data"> I am sending the date to Jango. Before testing with the date I get, I’m already humping a date I’m generating. Nor does it work: minhamodel.data_resgate = str(datetime.datetime.now().date()).

  • My field is a DateField: data_resgate = models.DateField('Data Resgate', null=True, blank=True, default=datetime.datetime.now().date())

  • I’m with USE_L10N = True

  • No problem, I’m sure we’ll fix it. Come on: when you do minhamodel.data_resgate = str(datetime.datetime.now().date()), which of the two errors cited in the question do you receive? The TypeError or the ValueError? Also try to do this same test of this line without the cast to str and let me know the results.

  • I ran the same identical line here, with the exact same field in the models and successfully inserted, including with the USE_L10N = True. Any differentiated date-related settings you may have in your Settings.py other than this one?

  • Typeerror. It peded string or byte Object. Very strange. I’m also finding that it has nothing related to USE_L10N, but in creating the model or something in my database.

  • Tried without the cast to str? Take a look also directly at the database and see what kind of data it has created for this column. Maybe some problem with migrations?

  • I found out. I was creating a record but didn’t have a default for date. And as I was allowing null and Blank, I was giving an error right at the beginning. Just adding a default datetime.now() that worked.

  • 1

    Glad you could! It really didn’t even cross my mind. I thought the mistake I would make in this case would be quite different!

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.