From e2385e3c535cc7f48200d8d0bd3fea61eafcb98e Mon Sep 17 00:00:00 2001 From: Jason Pellerin Date: Thu, 20 Jul 2006 15:27:49 +0000 Subject: [PATCH] [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 --- django/db/models/fields/__init__.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index f99f555625..86584b14e9 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -473,13 +473,19 @@ class DateTimeField(DateField): except ValueError: raise validators.ValidationError, gettext('Enter a valid date/time in YYYY-MM-DD HH:MM format.') - def get_db_prep_save(self, value): - # Casts dates into string format for entry into database. + def pre_save(self, model_instance, add): + value = super(DateField, 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): + # Casts dates into string format for entry into database. + if value is not None: value = str(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): value = datetime.datetime.now().time() setattr(model_instance, self.attname, value) - return value 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): # Casts dates into string format for entry into database. 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) return Field.get_db_prep_save(self, value)