Ticket #18044: django-efficient-save.patch
File django-efficient-save.patch, 2.7 KB (added by , 13 years ago) |
---|
-
django/db/models/sql/compiler.py
944 944 result = ['UPDATE %s' % qn(table)] 945 945 result.append('SET') 946 946 values, update_params = [], [] 947 947 948 for field, model, val in self.query.values: 949 if model and field.name not in model._state.update_fields: 950 continue 951 948 952 if hasattr(val, 'prepare_database_save'): 949 953 val = val.prepare_database_save(field) 950 954 else: -
django/db/models/base.py
260 260 261 261 signals.class_prepared.send(sender=cls) 262 262 263 263 264 class ModelState(object): 264 265 """ 265 266 A class for storing instance state … … 270 271 # Necessary for correct validation of new instances of objects with explicit (non-auto) PKs. 271 272 # This impacts validation only; it has no effect on the actual save. 272 273 self.adding = True 274 self.update_fields = {} 273 275 276 274 277 class Model(object): 275 278 __metaclass__ = ModelBase 276 279 _deferred = False … … 365 368 pass 366 369 if kwargs: 367 370 raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]) 371 368 372 super(Model, self).__init__() 369 373 signals.post_init.send(sender=self.__class__, instance=self) 370 374 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 371 388 def __repr__(self): 372 389 try: 373 390 u = unicode(self) … … 524 541 manager.using(using).filter(pk=pk_val).exists())): 525 542 # It does already exist, so do an UPDATE. 526 543 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] 528 545 if values: 529 546 rows = manager.using(using).filter(pk=pk_val)._update(values) 530 547 if force_update and not rows: