Code

Ticket #1584: auto_now_add_fix.patch

File auto_now_add_fix.patch, 3.8 KB (added by lukeplant, 8 years ago)

Patch involving reworked Field.pre_save()

  • django/db/models/base.py

     
    161161                (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val]) 
    162162            # If it does already exist, do an UPDATE. 
    163163            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] 
    165165                cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % \ 
    166166                    (backend.quote_name(self._meta.db_table), 
    167167                    ','.join(['%s=%%s' % backend.quote_name(f.column) for f in non_pks]), 
     
    171171                record_exists = False 
    172172        if not pk_set or not record_exists: 
    173173            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)] 
    175175            # If the PK has been manually set, respect that. 
    176176            if pk_set: 
    177177                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)] 
    179179            placeholders = ['%s'] * len(field_names) 
    180180            if self._meta.order_with_respect_to: 
    181181                field_names.append(backend.quote_name('_order')) 
  • django/db/models/fields/__init__.py

     
    152152    def get_internal_type(self): 
    153153        return self.__class__.__name__ 
    154154 
    155     def pre_save(self, value, add): 
     155    def pre_save(self, model_instance, add): 
    156156        "Returns field's value just before saving." 
    157         return value 
     157        return getattr(model_instance, self.attname) 
    158158 
    159159    def get_db_prep_save(self, value): 
    160160        "Returns field's value prepared for saving into a database." 
     
    417417            value = str(value) 
    418418        return Field.get_db_prep_lookup(self, lookup_type, value) 
    419419 
    420     def pre_save(self, value, add): 
     420    def pre_save(self, model_instance, add): 
    421421        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) 
    424427 
    425428    def contribute_to_class(self, cls, name): 
    426429        super(DateField,self).contribute_to_class(cls, name) 
     
    723726            value = str(value) 
    724727        return Field.get_db_prep_lookup(self, lookup_type, value) 
    725728 
    726     def pre_save(self, value, add): 
     729    def pre_save(self, model_instance, add): 
    727730        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) 
    730736 
    731737    def get_db_prep_save(self, value): 
    732738        # Casts dates into string format for entry into database.