mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -161,7 +161,7 @@ class Model(object): | |||||||
|                 (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val]) |                 (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 it does already exist, do an UPDATE. | ||||||
|             if cursor.fetchone(): |             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" % \ |                 cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % \ | ||||||
|                     (backend.quote_name(self._meta.db_table), |                     (backend.quote_name(self._meta.db_table), | ||||||
|                     ','.join(['%s=%%s' % backend.quote_name(f.column) for f in non_pks]), |                     ','.join(['%s=%%s' % backend.quote_name(f.column) for f in non_pks]), | ||||||
| @@ -171,11 +171,11 @@ class Model(object): | |||||||
|                 record_exists = False |                 record_exists = False | ||||||
|         if not pk_set or not record_exists: |         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)] |             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 the PK has been manually set, respect that. | ||||||
|             if pk_set: |             if pk_set: | ||||||
|                 field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)] |                 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) |             placeholders = ['%s'] * len(field_names) | ||||||
|             if self._meta.order_with_respect_to: |             if self._meta.order_with_respect_to: | ||||||
|                 field_names.append(backend.quote_name('_order')) |                 field_names.append(backend.quote_name('_order')) | ||||||
|   | |||||||
| @@ -152,9 +152,9 @@ class Field(object): | |||||||
|     def get_internal_type(self): |     def get_internal_type(self): | ||||||
|         return self.__class__.__name__ |         return self.__class__.__name__ | ||||||
|  |  | ||||||
|     def pre_save(self, value, add): |     def pre_save(self, model_instance, add): | ||||||
|         "Returns field's value just before saving." |         "Returns field's value just before saving." | ||||||
|         return value |         return getattr(model_instance, self.attname) | ||||||
|  |  | ||||||
|     def get_db_prep_save(self, value): |     def get_db_prep_save(self, value): | ||||||
|         "Returns field's value prepared for saving into a database." |         "Returns field's value prepared for saving into a database." | ||||||
| @@ -417,10 +417,13 @@ class DateField(Field): | |||||||
|             value = str(value) |             value = str(value) | ||||||
|         return Field.get_db_prep_lookup(self, lookup_type, 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): |         if self.auto_now or (self.auto_now_add and add): | ||||||
|             return datetime.datetime.now() |             value = datetime.datetime.now() | ||||||
|  |             setattr(model_instance, self.attname, value) | ||||||
|             return value |             return value | ||||||
|  |         else: | ||||||
|  |             return super(DateField, self).pre_save(model_instance, add) | ||||||
|  |  | ||||||
|     def contribute_to_class(self, cls, name): |     def contribute_to_class(self, cls, name): | ||||||
|         super(DateField,self).contribute_to_class(cls, name) |         super(DateField,self).contribute_to_class(cls, name) | ||||||
| @@ -723,10 +726,13 @@ class TimeField(Field): | |||||||
|             value = str(value) |             value = str(value) | ||||||
|         return Field.get_db_prep_lookup(self, lookup_type, 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): |         if self.auto_now or (self.auto_now_add and add): | ||||||
|             return datetime.datetime.now().time() |             value = datetime.datetime.now().time() | ||||||
|  |             setattr(model_instance, self.attname, value) | ||||||
|             return value |             return value | ||||||
|  |         else: | ||||||
|  |             return super(TimeField, self).pre_save(model_instance, add) | ||||||
|  |  | ||||||
|     def get_db_prep_save(self, value): |     def get_db_prep_save(self, value): | ||||||
|         # Casts dates into string format for entry into database. |         # Casts dates into string format for entry into database. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user