Ticket #18044: django-efficient-save.patch

File django-efficient-save.patch, 2.7 KB (added by Andrei Antoukh, 12 years ago)
  • django/db/models/sql/compiler.py

     
    944944        result = ['UPDATE %s' % qn(table)]
    945945        result.append('SET')
    946946        values, update_params = [], []
     947
    947948        for field, model, val in self.query.values:
     949            if model and field.name not in model._state.update_fields:
     950                continue
     951
    948952            if hasattr(val, 'prepare_database_save'):
    949953                val = val.prepare_database_save(field)
    950954            else:
  • django/db/models/base.py

     
    260260
    261261        signals.class_prepared.send(sender=cls)
    262262
     263
    263264class ModelState(object):
    264265    """
    265266    A class for storing instance state
     
    270271        # Necessary for correct validation of new instances of objects with explicit (non-auto) PKs.
    271272        # This impacts validation only; it has no effect on the actual save.
    272273        self.adding = True
     274        self.update_fields = {}
    273275
     276
    274277class Model(object):
    275278    __metaclass__ = ModelBase
    276279    _deferred = False
     
    365368                    pass
    366369            if kwargs:
    367370                raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
     371
    368372        super(Model, self).__init__()
    369373        signals.post_init.send(sender=self.__class__, instance=self)
    370374
     375    def __setattr__(self, name, value):
     376        is_for_update = False
     377
     378        if hasattr(self, name):
     379            is_for_update = True
     380
     381        super(Model, self).__setattr__(name, value)
     382       
     383        if is_for_update:
     384            local_fields = {x.name:x for x in self._meta.fields}
     385            if name in local_fields:
     386                self._state.update_fields.update({name:local_fields[name]})
     387
    371388    def __repr__(self):
    372389        try:
    373390            u = unicode(self)
     
    524541                        manager.using(using).filter(pk=pk_val).exists())):
    525542                    # It does already exist, so do an UPDATE.
    526543                    if force_update or non_pks:
    527                         values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
     544                        values = [(f, self, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
    528545                        if values:
    529546                            rows = manager.using(using).filter(pk=pk_val)._update(values)
    530547                            if force_update and not rows:
Back to Top