Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't receive post_save signal when bulk update instance #25

Open
peijianbo opened this issue Jul 26, 2021 · 5 comments
Open

Can't receive post_save signal when bulk update instance #25

peijianbo opened this issue Jul 26, 2021 · 5 comments

Comments

@peijianbo
Copy link

Can't receive post_save signal when bulk update instance

@fopina
Copy link
Owner

fopina commented Jul 26, 2021

bulk_create and bulk_update do not use save (nor send the signal), bulk_update_or_create doesn’t either.
I’ll leave this open to remember to update the docs to highlight this, thanks

@peijianbo
Copy link
Author

bulk_create and bulk_update do not use save (nor send the signal), bulk_update_or_create doesn’t either.
I’ll leave this open to remember to update the docs to highlight this, thanks

I found post_signal was received when create an object , but there is no post_signal when update object. because you called model save method and bulk_update method in you code.
and Whether the signal can be unified?both actions send a signal or both not send a signal.

That is you code:

    for batch in batches:
        obj_map = {_obj_key_getter(obj): obj for obj in batch}
        # mass select for bulk_update on existing ones
        to_update = self.filter(_obj_filter(obj_map))
        for to_u in to_update:
            obj = obj_map[_obj_key_getter(to_u)]
            for _f in update_fields:
                setattr(to_u, _f, getattr(obj, _f))
            del obj_map[_obj_key_getter(to_u)]
        self.bulk_update(to_update, update_fields)
        # .create on the remaining (bulk_create won't work on multi-table inheritance models...)
        created_objs = []
        for obj in obj_map.values():
            obj.save()
            created_objs.append(obj)
        if yield_objects:
            yield created_objs, to_update

@fopina
Copy link
Owner

fopina commented Jul 29, 2021

Yes the comment explains why that happens:
# .create on the remaining (bulk_create won't work on multi-table inheritance models...)

bulk_create doesn’t work with multi-table models (but bulk_update does and it’s the main improvement of the package).
if bulk_create was used it wouldn’t send the signal either.
I’ll try to document that behavior.

@peijianbo
Copy link
Author

behavior

are you chinese?

@fopina
Copy link
Owner

fopina commented Jul 29, 2021

No, not sure I understood though. Chinese use American spelling?:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants