get_db_prep_save should attempt conversion from strings
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
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))