Code

Ticket #4102: 4102_django-1.1.patch

File 4102_django-1.1.patch, 2.0 KB (added by seandong, 5 years ago)

patch for django1.1

  • django/db/models/base.py

     
    240240class Model(object): 
    241241    __metaclass__ = ModelBase 
    242242    _deferred = False 
     243     
     244    def __setattr__(self, name, value): 
     245        if name not in self._modified_attrs: 
     246            if not hasattr(self, name) or value != getattr(self, name): 
     247                self._modified_attrs.append(name) 
     248        super(Model, self).__setattr__(name, value) 
     249     
     250    def _reset_modified_attrs(self): 
     251        self.__dict__['_modified_attrs'] = [] 
    243252 
    244253    def __init__(self, *args, **kwargs): 
     254        self._reset_modified_attrs() 
    245255        signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs) 
    246256 
    247257        # There is a rather weird disparity here; if kwargs, it's set, then args 
     
    458468 
    459469        if not meta.proxy: 
    460470            non_pks = [f for f in meta.local_fields if not f.primary_key] 
    461  
     471            modified_attrs = self._modified_attrs 
     472            non_pks = [f for f in non_pks if (f.name in modified_attrs or f.attname in modified_attrs)] 
     473            self._reset_modified_attrs() 
     474             
    462475            # First, try an UPDATE. If that doesn't update anything, do an INSERT. 
    463476            pk_val = self._get_pk_val(meta) 
    464477            pk_set = pk_val is not None 
  • django/db/models/query.py

     
    249249                else: 
    250250                    # Omit aggregates in object creation. 
    251251                    obj = self.model(*row[index_start:aggregate_start]) 
     252                    # Models keep a track of modified attrs to choose which 
     253                    # fields to save. Since we're just pulling from the  
     254                    # database, nothing has changed yet.  
     255                    obj._reset_modified_attrs() 
    252256 
    253257            for i, k in enumerate(extra_select): 
    254258                setattr(obj, k, row[i])