johnpaulett / django-durationfield

Temporary reusable Django application for adding a DurationField, until Django issue #2443 is resolved.
http://django-durationfield.readthedocs.org
67 stars 24 forks source link

get_db_prep_save should attempt conversion from strings #2

Open Semprini opened 13 years ago

Semprini commented 13 years ago

When migrating from previous implementations of DurationField I found my json data dump fields to be encapsulated by quotes. When loading the data into the new schema this causes the value parameter to be passed as unicode. I put in the following fix:

def get_db_prep_save(self, value): if value is None: return None # db NULL if isinstance(value, int) or isinstance(value, long): value = timedelta(microseconds=value) elif isinstance(value,unicode) or isinstance(value, str): value = timedelta(microseconds=int(value)) return value.days * 24 * 3600 * 1000000 + value.seconds * 1000000 + value.microseconds

kavdev commented 9 years ago

It's definitely nice to have that check. I suggest using six.string_types instead:

elif isinstance(value, six.string_types):
    value = timedelta(microseconds=int(value))