django-bulk-update-or-create icon indicating copy to clipboard operation
django-bulk-update-or-create copied to clipboard

Can't receive post_save signal when bulk update instance

Open peijianbo opened this issue 2 years ago • 5 comments

Can't receive post_save signal when bulk update instance

peijianbo avatar Jul 26 '21 01:07 peijianbo

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

fopina avatar Jul 26 '21 05:07 fopina

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

peijianbo avatar Jul 29 '21 10:07 peijianbo

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.

fopina avatar Jul 29 '21 11:07 fopina

behavior

are you chinese?

peijianbo avatar Jul 29 '21 11:07 peijianbo

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

fopina avatar Jul 29 '21 11:07 fopina