Django:分辨率为微秒的DurationField
django DurationField在django管理界面中仅显示HH:MM:SS 不幸的是,在我目前的环境下,这还不够 我需要能够在管理界面中显示/编辑微秒 这怎么可能呢 更新 这是一个错误。我在数据库中的数据是错误的。在数据进入数据库之前,在进程中被删除的微秒数 Django显示微秒(如果有)。您不需要做任何事情来显示它们。查看源代码: 我认为方法是重写Django:分辨率为微秒的DurationField,django,datetime,django-admin,Django,Datetime,Django Admin,django DurationField在django管理界面中仅显示HH:MM:SS 不幸的是,在我目前的环境下,这还不够 我需要能够在管理界面中显示/编辑微秒 这怎么可能呢 更新 这是一个错误。我在数据库中的数据是错误的。在数据进入数据库之前,在进程中被删除的微秒数 Django显示微秒(如果有)。您不需要做任何事情来显示它们。查看源代码: 我认为方法是重写forms.DurationField(),确切地说,这些方法: 来自django.utils.duration导入duration\
forms.DurationField
(),确切地说,这些方法:
来自django.utils.duration导入duration\u字符串
def duration_string(duration):
"""Version of str(timedelta) which is not English specific."""
days, hours, minutes, seconds, microseconds = _get_duration_components(duration)
string = '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
if days:
string = '{} '.format(days) + string
if microseconds:
string += '.{:06d}'.format(microseconds)
return string
请注意,可能也需要覆盖这些django.utils.dateparse.parse\u duration
def parse_duration(value):
"""Parse a duration string and return a datetime.timedelta.
The preferred format for durations in Django is '%d %H:%M:%S.%f'.
Also supports ISO 8601 representation and PostgreSQL's day-time interval
format.
"""
match = standard_duration_re.match(value)
if not match:
match = iso8601_duration_re.match(value) or postgres_interval_re.match(value)
if match:
kw = match.groupdict()
days = datetime.timedelta(float(kw.pop('days', 0) or 0))
sign = -1 if kw.pop('sign', '+') == '-' else 1
if kw.get('microseconds'):
kw['microseconds'] = kw['microseconds'].ljust(6, '0')
if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'):
kw['microseconds'] = '-' + kw['microseconds']
kw = {k: float(v) for k, v in kw.items() if v is not None}
return days + sign * datetime.timedelta(**kw)