Thursday, 15 September 2011

Django Migrate - change charfield to datetimefield -



Django Migrate - change charfield to datetimefield -

i have charfield in model wanted alter datetimefield.

from

days = models.charfield(max_length=100)

to

days = models.datetimefield(blank=true, null=true)

but when using migrate command, gives me error.

django.db.utils.programmingerror: column "days" cannot cast type timestamp time zone

i using django 1.7.

django's approach timezone handling convert datetimes utc , store them in database without timezone information. apparently charfields include timezone field.

one approach edit migrations file produced makemigrations , insert runpython command first operation. utilize convert charfields string utc version without timezones. might this:

from django.utils.dateparse import parse_datetime django.utils import timezone days_aware = parse_datetime(days) days_utc = days_aware.astimezone(timezone.utc) days_naive = days_utc.replace(tzinfo=none) days = str(days_naive)

alternatively, instead of changing string representation, instead create datetimefield new field (say days_dt); runpython info migration convert string datetime (days_dt = parse_datetime(days)); delete days field; , rename days_dt days. work , leave django in charge of representing datetimes @ database level.

django django-models migration

No comments:

Post a Comment