Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #1584 - auto_now_add fields now work when saving multiple times.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3002 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 037f8d580407ecf346041a4cbc7f7bf9492378a4 1 parent ea325ad
@spookylukey spookylukey authored
Showing with 17 additions and 11 deletions.
  1. +3 −3 django/db/models/base.py
  2. +14 −8 django/db/models/fields/__init__.py
View
6 django/db/models/base.py
@@ -161,7 +161,7 @@ def save(self):
(backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
# If it does already exist, do an UPDATE.
if cursor.fetchone():
- db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), False)) for f in non_pks]
+ db_values = [f.get_db_prep_save(f.pre_save(self, False)) for f in non_pks]
cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % \
(backend.quote_name(self._meta.db_table),
','.join(['%s=%%s' % backend.quote_name(f.column) for f in non_pks]),
@@ -171,11 +171,11 @@ def save(self):
record_exists = False
if not pk_set or not record_exists:
field_names = [backend.quote_name(f.column) for f in self._meta.fields if not isinstance(f, AutoField)]
- db_values = [f.get_db_prep_save(f.pre_save(getattr(self, f.attname), True)) for f in self._meta.fields if not isinstance(f, AutoField)]
+ db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]
# If the PK has been manually set, respect that.
if pk_set:
field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)]
- db_values += [f.get_db_prep_save(f.pre_save(getattr(self, f.column), True)) for f in self._meta.fields if isinstance(f, AutoField)]
+ db_values += [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)]
placeholders = ['%s'] * len(field_names)
if self._meta.order_with_respect_to:
field_names.append(backend.quote_name('_order'))
View
22 django/db/models/fields/__init__.py
@@ -152,9 +152,9 @@ def get_cache_name(self):
def get_internal_type(self):
return self.__class__.__name__
- def pre_save(self, value, add):
+ def pre_save(self, model_instance, add):
"Returns field's value just before saving."
- return value
+ return getattr(model_instance, self.attname)
def get_db_prep_save(self, value):
"Returns field's value prepared for saving into a database."
@@ -417,10 +417,13 @@ def get_db_prep_lookup(self, lookup_type, value):
value = str(value)
return Field.get_db_prep_lookup(self, lookup_type, value)
- def pre_save(self, value, add):
+ def pre_save(self, model_instance, add):
if self.auto_now or (self.auto_now_add and add):
- return datetime.datetime.now()
- return value
+ value = datetime.datetime.now()
+ setattr(model_instance, self.attname, value)
+ return value
+ else:
+ return super(DateField, self).pre_save(model_instance, add)
def contribute_to_class(self, cls, name):
super(DateField,self).contribute_to_class(cls, name)
@@ -723,10 +726,13 @@ def get_db_prep_lookup(self, lookup_type, value):
value = str(value)
return Field.get_db_prep_lookup(self, lookup_type, value)
- def pre_save(self, value, add):
+ def pre_save(self, model_instance, add):
if self.auto_now or (self.auto_now_add and add):
- return datetime.datetime.now().time()
- return value
+ 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.
Please sign in to comment.
Something went wrong with that request. Please try again.