Ticket #13021: improved_update.patch

File improved_update.patch, 3.0 KB (added by Dennis Kaarsemaker, 13 years ago)
  • django/db/models/base.py

     
    539539
    540540    save_base.alters_data = True
    541541
     542    @classmethod
     543    def update(klass, queryset, updates, using=None):
     544        for field in klass._meta.fields:
     545            field.pre_update(queryset, updates)
     546
    542547    def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
    543548        """
    544549        Recursively populates seen_objs with all objects related to this
  • django/db/models/fields/__init__.py

     
    282282        "Returns field's value just before saving."
    283283        return getattr(model_instance, self.attname)
    284284
     285    def pre_update(self, queryset, kwargs):
     286        "Do nothing by default"
     287        pass
     288
    285289    def get_prep_value(self, value):
    286290        "Perform preliminary non-db specific value checks and conversions."
    287291        return value
     
    649653        else:
    650654            return super(DateField, self).pre_save(model_instance, add)
    651655
     656    def pre_update(self, queryset, kwargs):
     657        if self.auto_now and (self.name not in kwargs):
     658            kwargs[self.name] =  datetime.datetime.now()
     659
    652660    def contribute_to_class(self, cls, name):
    653661        super(DateField,self).contribute_to_class(cls, name)
    654662        if not self.null:
  • django/db/models/signals.py

     
    1414post_syncdb = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive"])
    1515
    1616m2m_changed = Signal(providing_args=["action", "instance", "reverse", "model", "pk_set"])
     17
     18pre_update = Signal(providing_args=["queryset", "updates"])
     19post_update = Signal(providing_args=["queryset", "updates"])
  • django/db/models/query.py

     
    450450        """
    451451        assert self.query.can_filter(), \
    452452                "Cannot update a query once a slice has been taken."
     453        signals.pre_update.send(sender=self.model, queryset=self, updates=kwargs)
    453454        self._for_write = True
    454455        query = self.query.clone(sql.UpdateQuery)
     456        self.model.update(self, kwargs)
    455457        query.add_update_values(kwargs)
    456458        if not transaction.is_managed(using=self.db):
    457459            transaction.enter_transaction_management(using=self.db)
     
    468470            if forced_managed:
    469471                transaction.leave_transaction_management(using=self.db)
    470472        self._result_cache = None
     473        signals.post_update.send(sender=self.model, queryset=self, updates=kwargs)
    471474        return rows
    472475    update.alters_data = True
    473476
Back to Top