Ticket #1584: auto_now_add_fix.patch
File auto_now_add_fix.patch, 3.8 KB (added by , 18 years ago) |
---|
-
django/db/models/base.py
161 161 (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val]) 162 162 # If it does already exist, do an UPDATE. 163 163 if cursor.fetchone(): 164 db_values = [f.get_db_prep_save(f.pre_save( getattr(self, f.attname), False)) for f in non_pks]164 db_values = [f.get_db_prep_save(f.pre_save(self, False)) for f in non_pks] 165 165 cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % \ 166 166 (backend.quote_name(self._meta.db_table), 167 167 ','.join(['%s=%%s' % backend.quote_name(f.column) for f in non_pks]), … … 171 171 record_exists = False 172 172 if not pk_set or not record_exists: 173 173 field_names = [backend.quote_name(f.column) for f in self._meta.fields if not isinstance(f, AutoField)] 174 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)]174 db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)] 175 175 # If the PK has been manually set, respect that. 176 176 if pk_set: 177 177 field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)] 178 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)]178 db_values += [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)] 179 179 placeholders = ['%s'] * len(field_names) 180 180 if self._meta.order_with_respect_to: 181 181 field_names.append(backend.quote_name('_order')) -
django/db/models/fields/__init__.py
152 152 def get_internal_type(self): 153 153 return self.__class__.__name__ 154 154 155 def pre_save(self, value, add):155 def pre_save(self, model_instance, add): 156 156 "Returns field's value just before saving." 157 return value157 return getattr(model_instance, self.attname) 158 158 159 159 def get_db_prep_save(self, value): 160 160 "Returns field's value prepared for saving into a database." … … 417 417 value = str(value) 418 418 return Field.get_db_prep_lookup(self, lookup_type, value) 419 419 420 def pre_save(self, value, add):420 def pre_save(self, model_instance, add): 421 421 if self.auto_now or (self.auto_now_add and add): 422 return datetime.datetime.now() 423 return value 422 value = datetime.datetime.now() 423 setattr(model_instance, self.attname, value) 424 return value 425 else: 426 return super(DateField, self).pre_save(model_instance, add) 424 427 425 428 def contribute_to_class(self, cls, name): 426 429 super(DateField,self).contribute_to_class(cls, name) … … 723 726 value = str(value) 724 727 return Field.get_db_prep_lookup(self, lookup_type, value) 725 728 726 def pre_save(self, value, add):729 def pre_save(self, model_instance, add): 727 730 if self.auto_now or (self.auto_now_add and add): 728 return datetime.datetime.now().time() 729 return value 731 value = datetime.datetime.now().time() 732 setattr(model_instance, self.attname, value) 733 return value 734 else: 735 return super(TimeField, self).pre_save(model_instance, add) 730 736 731 737 def get_db_prep_save(self, value): 732 738 # Casts dates into string format for entry into database.