[PonyORM-list] Disable after_insert hook?

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Tue Sep 13 17:24:17 UTC 2016


Hi Matthew,

If you only want to disable after_insert hook in a single entity class for
the duration of the script execution, you can just delete the method as you
suggested.

If you want to temporary disable after_insert hook in all entity classes
you can use the following context manager, which temporary overrides
Entity._after_save_ internal method:

from contextlib import contextmanager
from pony.orm.core import Entity

@contextmanager
def disable_after_insert_hooks():
prev_after_save = Entity._after_save_
try:
def after_save(obj, status):
if status == 'inserted':
pass # do nothing
else:
prev_after_save(obj, status)
Entity._after_save_ = after_save
yield
finally:
Entity._after_save_ = prev_after_save

Usage:

    with disable_after_insert_hooks(), db_session:
        <do something>


Regards,
Alexander


On Tue, Sep 13, 2016 at 7:57 PM, Matthew Bell <matthewrobertbell at gmail.com>
wrote:

> Of course, as soon as I send the email, I figure it out :)
>
> del MyModel.after_insert
>
> (The method won't be needed again in this process)
>
> On 13 September 2016 at 17:40, Matthew Bell <matthewrobertbell at gmail.com>
> wrote:
>
>> Hello,
>>
>> For a very specific circumstance, I need to disable the after_insert hook
>> on a pony model, is there a clean way of doing this?
>>
>> Thanks!
>>
>> --
>> Regards,
>>
>> Matthew Bell
>>
>
>
>
> --
> Regards,
>
> Matthew Bell
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20160913/0d6385af/attachment.html>


More information about the ponyorm-list mailing list