diff options
| author | Jason Pellerin <jpellerin@gmail.com> | 2006-07-20 15:27:49 +0000 |
|---|---|---|
| committer | Jason Pellerin <jpellerin@gmail.com> | 2006-07-20 15:27:49 +0000 |
| commit | e2385e3c535cc7f48200d8d0bd3fea61eafcb98e (patch) | |
| tree | 58275b4088a62db695b0bd6fe8f5b62b42f070f0 | |
| parent | a6064b22cf3c2b475abad8f0a96dc39f8a0d49b5 (diff) | |
[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
| -rw-r--r-- | django/db/models/fields/__init__.py | 24 |
1 files 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) - - def get_db_prep_save(self, value): - # Casts dates into string format for entry into database. + 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. - if settings.DATABASE_ENGINE == 'mysql': + 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) |
