1
0
mirror of https://github.com/django/django.git synced 2025-07-05 18:29:11 +00:00

[multi-db] Moved DateTimeField and TimeField microsecond adjustments

from get_db_prep_save to pre_save, since they depend on database 
settings and get_db_prep_save does not have access to the model instance 
to which the field is bound.


git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3394 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jason Pellerin 2006-07-20 15:27:49 +00:00
parent a6064b22cf
commit e2385e3c53

View File

@ -473,13 +473,19 @@ class DateTimeField(DateField):
except ValueError: except ValueError:
raise validators.ValidationError, gettext('Enter a valid date/time in YYYY-MM-DD HH:MM format.') raise validators.ValidationError, gettext('Enter a valid date/time in YYYY-MM-DD HH:MM format.')
def get_db_prep_save(self, value): def pre_save(self, model_instance, add):
# Casts dates into string format for entry into database. value = super(DateField, self).pre_save(model_instance, add)
if value is not None: if value is not None:
# MySQL will throw a warning if microseconds are given, because it # MySQL will throw a warning if microseconds are given, because it
# doesn't support microseconds. # doesn't support microseconds.
settings = model_instance._default_manager.db.connection.settings
if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'): if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
value = value.replace(microsecond=0) value = value.replace(microsecond=0)
return value
def get_db_prep_save(self, value):
# Casts dates into string format for entry into database.
if value is not None:
value = str(value) value = str(value)
return Field.get_db_prep_save(self, value) return Field.get_db_prep_save(self, value)
@ -733,17 +739,19 @@ class TimeField(Field):
if self.auto_now or (self.auto_now_add and add): if self.auto_now or (self.auto_now_add and add):
value = datetime.datetime.now().time() value = datetime.datetime.now().time()
setattr(model_instance, self.attname, value) setattr(model_instance, self.attname, value)
return value
else: else:
return super(TimeField, self).pre_save(model_instance, add) value = super(TimeField, self).pre_save(model_instance, add)
if value is not None:
# MySQL will throw a warning if microseconds are given, because it
# doesn't support microseconds.
settings = model_instance._default_manager.db.connection.settings
if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'):
value = value.replace(microsecond=0)
return value
def get_db_prep_save(self, value): def get_db_prep_save(self, value):
# Casts dates into string format for entry into database. # Casts dates into string format for entry into database.
if value is not None: if value is not None:
# MySQL will throw a warning if microseconds are given, because it
# doesn't support microseconds.
if settings.DATABASE_ENGINE == 'mysql':
value = value.replace(microsecond=0)
value = str(value) value = str(value)
return Field.get_db_prep_save(self, value) return Field.get_db_prep_save(self, value)