Code

Ticket #4102: 4102-dirty.patch

File 4102-dirty.patch, 1.7 KB (added by Collin Grady <cgrady@…>, 7 years ago)

initial shot at 'dirty' flag, seems to work so far

  • django/db/models/base.py

     
    9494    def __ne__(self, other): 
    9595        return not self.__eq__(other) 
    9696 
     97    _dirty = {} 
     98 
     99    def __setattr__(self, name, value): 
     100        if name != '_dirty': 
     101            self._dirty[name] = True 
     102        super(Model, self).__setattr__(name, value) 
     103 
     104    def _wash(self): 
     105        self._dirty = {} 
     106 
    97107    def __init__(self, *args, **kwargs): 
    98108        dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs) 
    99          
     109 
    100110        # There is a rather weird disparity here; if kwargs, it's set, then args 
    101111        # overrides it. It should be one or the other; don't duplicate the work  
    102112        # The reason for the kwargs check is that standard iterator passes in by 
     
    164174                    pass 
    165175            if kwargs: 
    166176                raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0] 
     177 
     178        self._wash() 
    167179        dispatcher.send(signal=signals.post_init, sender=self.__class__, instance=self) 
    168180 
    169181    def add_to_class(cls, name, value): 
     
    201213    def save(self): 
    202214        dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self) 
    203215 
    204         non_pks = [f for f in self._meta.fields if not f.primary_key] 
     216        non_pks = [f for f in self._meta.fields if not f.primary_key and (self._dirty.get(f.name, False) or self._dirty.get(f.attname, False))] 
     217        self._wash() 
    205218        cursor = connection.cursor() 
    206219 
    207220        # First, try an UPDATE. If that doesn't update anything, do an INSERT.