fopina / django-bulk-update-or-create

`bulk_update_or_create` for Django model managers
MIT License
148 stars 16 forks source link

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

Open peijianbo opened 3 years ago

peijianbo commented 3 years ago

Can't receive post_save signal when bulk update instance

fopina commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

behavior

are you chinese?

fopina commented 3 years ago

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