Search code examples
pythondjangodatetimedjango-modelstraceback

DataError for a DateTime?


I have a Django model (ReportCache) that stores reports in an SQL table. One of the fields in it is a django.db.models.DateTimeField, used for storing the last time the report was updated.

For some reason, I'm getting a DataError: value too long for type character varying(20) when trying to save a datetime to them. The datetime is generated by datetime.datetime.now(), and I have tried with and without django.utils.timezone.make_aware(), with no affect.

Full traceback is at https://gist.github.com/cyberjacob/2f1e61f83a6fbd5792b8

Any suggestions why my date is randomly too long?


Solution

  • The problem does not seem to be with the date field, but with the ReportKey string field.

    As mentioned, it is not idiomatic in Python to call the double-underscore methods like foo.__str__() directly. You should call the built-in functions: str(foo). Even better though is to use string interpolation:

    ReportKey = "DetailedReport.{}.{}.{}".format(year, month, clientId)