Django DateTimeField TypeError: expected string or

2019-07-27 03:35发布

问题:

I know there are many posts about this error. Such as: this and this one

But I knew already that. And that's why I'm getting crazy.

When I create an instance and try to save it, I don't have problems:

per_detail.preview_title = per_detail.details_sample.preview_title
per_detail.icon = per_detail.details_sample.icon
per_detail.content = fill_content(per_detail)
per_detail.save()

easy.

But when there's already the instance, and I try to save it again (updated it). Then I get this error:

match = datetime_re.match(value)

TypeError: expected string or bytes-like object

whit this code:

personal_detail_sample = kwargs['instance']
        personal_details = PersonalDetail.objects.filter(Q(details_sample=personal_detail_sample))
        for per_detail in personal_details:
            per_detail_updated = fill_updated_content(personal_detail_sample, per_detail)
            per_detail_updated.save()

I gotta say, it doesn't matter where I try to update that instance, I get always the same error. (so, it's not because the kwargs['instance'])

And here's the field, that is giving troubles:

sent_date = models.DateTimeField(_('sent_date'), null=True, blank=True)

As you maybe noticed, I never filled the field "sent_date", but it should be null. So it shouldn't be a problem. And for just being safe, I also tried to do:

per_detail.sent_date = *a date*
per_detail.save()

And I'm getting the same error.

I don't have any idea what could it be.

Maybe someone can help me.

回答1:

I got an error in the field "date" not "sent_date".

I'm pretty sorry. The field of "date" in the models.py was:

date = models.DateTimeField(_('date'), auto_now_add=True) but in the DataBase was time without time zone and not timestamp without time zone. Why? Who knows...

I deleted the sent_date field, and saw, that there was another error with a date, and that's why I found the error.

Many thanks to the people that tried to help!