Ticket #8077: signals_patch3.diff

File signals_patch3.diff, 2.7 KB (added by brooks.travis@…, 16 years ago)

A new patch against trunk (8964) using the send_signals=False method.

  • django/db/models/base.py

     
    192192    __metaclass__ = ModelBase
    193193
    194194    def __init__(self, *args, **kwargs):
    195         signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs)
     195        if send_signals:
     196            signals.pre_init.send(sender=self.__class__, args=args, kwargs=kwargs)
    196197
    197198        # There is a rather weird disparity here; if kwargs, it's set, then args
    198199        # overrides it. It should be one or the other; don't duplicate the work
     
    263264                    pass
    264265            if kwargs:
    265266                raise TypeError, "'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]
    266         signals.post_init.send(sender=self.__class__, instance=self)
     267        if send_signals:
     268            signals.post_init.send(sender=self.__class__, instance=self)
    267269
    268270    def __repr__(self):
    269271        return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
     
    292294
    293295    pk = property(_get_pk_val, _set_pk_val)
    294296
    295     def save(self, force_insert=False, force_update=False):
     297    def save(self, send_signals=True, force_insert=False, force_update=False):
    296298        """
    297299        Saves the current instance. Override this in a subclass if you want to
    298300        control the saving process.
     
    304306        if force_insert and force_update:
    305307            raise ValueError("Cannot force both insert and updating in "
    306308                    "model saving.")
    307         self.save_base(force_insert=force_insert, force_update=force_update)
     309        self.save_base(send_signals=send_signals, force_insert=force_insert, force_update=force_update)
    308310
    309311    save.alters_data = True
    310312
    311     def save_base(self, raw=False, cls=None, force_insert=False,
     313    def save_base(self, send_signals=True, raw=False, cls=None, force_insert=False,
    312314            force_update=False):
    313315        """
    314316        Does the heavy-lifting involved in saving. Subclasses shouldn't need to
     
    321323            cls = self.__class__
    322324            meta = self._meta
    323325            signal = True
    324             signals.pre_save.send(sender=self.__class__, instance=self, raw=raw)
     326            if send_signals:
     327                signals.pre_save.send(sender=self.__class__, instance=self, raw=raw)
    325328        else:
    326329            meta = cls._meta
    327330            signal = False
     
    385388                setattr(self, meta.pk.attname, result)
    386389        transaction.commit_unless_managed()
    387390
    388         if signal:
     391        if signal and send_signals:
    389392            signals.post_save.send(sender=self.__class__, instance=self,
    390393                created=(not record_exists), raw=raw)
    391394
Back to Top