From dadfca08c0db567ce33284aaa8eb388cf667a836 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Wed, 8 Nov 2006 04:50:01 +0000 Subject: sqlalchemy: Merged revisions 3918 to 4053 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/sqlalchemy@4054 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'django/db/models/fields/__init__.py') diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index d82f38527d..bc1eaf0a6a 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -457,7 +457,9 @@ class DateField(Field): def get_db_prep_save(self, value): # Casts dates into string format for entry into database. - if value is not None: + if isinstance(value, datetime.datetime): + value = value.date().strftime('%Y-%m-%d') + elif isinstance(value, datetime.date): value = value.strftime('%Y-%m-%d') return Field.get_db_prep_save(self, value) @@ -487,12 +489,19 @@ class DateTimeField(DateField): def get_db_prep_save(self, value): # Casts dates into string format for entry into database. - if value is not None: + if isinstance(value, datetime.datetime): # MySQL will throw a warning if microseconds are given, because it # doesn't support microseconds. if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'): value = value.replace(microsecond=0) value = str(value) + elif isinstance(value, datetime.date): + # MySQL will throw a warning if microseconds are given, because it + # doesn't support microseconds. + if settings.DATABASE_ENGINE == 'mysql' and hasattr(value, 'microsecond'): + value = datetime.datetime(value.year, value.month, value.day, microsecond=0) + value = str(value) + return Field.get_db_prep_save(self, value) def get_db_prep_lookup(self, lookup_type, value): @@ -576,7 +585,7 @@ class FileField(Field): # If the raw path is passed in, validate it's under the MEDIA_ROOT. def isWithinMediaRoot(field_data, all_data): f = os.path.abspath(os.path.join(settings.MEDIA_ROOT, field_data)) - if not f.startswith(os.path.normpath(settings.MEDIA_ROOT)): + if not f.startswith(os.path.abspath(os.path.normpath(settings.MEDIA_ROOT))): raise validators.ValidationError, _("Enter a valid filename.") field_list[1].validator_list.append(isWithinMediaRoot) return field_list -- cgit v1.3