Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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
  • Loading branch information...
commit e2385e3c535cc7f48200d8d0bd3fea61eafcb98e 1 parent a6064b2
JP jpellerin authored
Showing with 16 additions and 8 deletions.
  1. +16 −8 django/db/models/fields/__init__.py
24 django/db/models/fields/__init__.py
View
@@ -473,13 +473,19 @@ def to_python(self, value):
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 @@ def pre_save(self, model_instance, add):
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)
Please sign in to comment.
Something went wrong with that request. Please try again.