2

I'm using a mysql 5.7 db and django 1.8 so as far as I understand milliseconds are supported by both of them. I can enter times in the form of HH:MM:SS.fff via mysql in to my db. (e.g. 00:05:45:100)

When I than open the admin page of django, it displays the time correctly (e.g. 00:05:45:100000) but if i enter the time via the admin form and press "save" the time becomes e.g. 00:05:45:000000 so the milliseconds don't get saved as it seems to me

Why is that and is there a way to change it? Thank you

admin.py

from django.contrib import admin from items.models import Maximum from django import forms class MaximumAdminForm(forms.ModelForm): def __init__(self, *arg, **kwargs): super(MaximumAdminForm, self).__init__(*arg, **kwargs) self.fields['time_point'] = forms.TimeField(widget=forms.TimeInput(format="%H:%M:%S.%f")) class Meta: model = Maximum fields = ['item', 'time_point',] class MaximumAdmin(admin.ModelAdmin): form = MaximumAdminForm def milliseconds(self, obj): return obj.time_point.strftime("%H:%M:%S.%f")[0:12] milliseconds.short_description = 'time point' list_display = ('id', 'milliseconds',) admin.site.register(Maximum, MaximumAdmin) 

1 Answer 1

4

The next stable release of Django 1.7 currently supports MySQL 5.0.3 and higher and not all of those versions support microseconds. Django 1.8 is currently the unstable master and its supported versions and release date have not been determined. Because of this the backend truncates microseconds in value_to_db_time:

def value_to_db_time(self, value): if value is None: return None # MySQL doesn't support tz-aware times if timezone.is_aware(value): raise ValueError("MySQL backend does not support timezone-aware times.") # MySQL doesn't support microseconds return six.text_type(value.replace(microsecond=0)) 

This is noted under the DateTime fields section of the supported databases docs:

MySQL does not store fractions of seconds. Fractions of seconds are truncated to zero when the time is stored.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.