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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Django's bulk create to improve notifications creation performance #283

Open
cbouvier15 opened this issue Apr 8, 2020 · 1 comment

Comments

@cbouvier15
Copy link

Hi there! 馃憢

I'm working on implementing a notifications sub-system for a project and because of that, I was analyzing in detail some parts of this great project.

So, when I saw the notify_handler method I noticed that, eventually, a bunch of notifications could be created and then saved within a for loop.

for recipient in recipients:
newnotify = Notification(
recipient=recipient,
actor_content_type=ContentType.objects.get_for_model(actor),
actor_object_id=actor.pk,
verb=str(verb),
public=public,
description=description,
timestamp=timestamp,
level=level,
)
# Set optional objects
for obj, opt in optional_objs:
if obj is not None:
setattr(newnotify, '%s_object_id' % opt, obj.pk)
setattr(newnotify, '%s_content_type' % opt,
ContentType.objects.get_for_model(obj))
if kwargs and EXTRA_DATA:
newnotify.data = kwargs
newnotify.save()
new_notifications.append(newnotify)

Do you guys think it would be possible to make use of Django's bulk create instead of calling save at every iteration?

I can work on it if you think it worth it.
Thanks!

@pandafy
Copy link
Contributor

pandafy commented Apr 19, 2020

I doubt that this will be viable, from Django docs:

The model鈥檚 save() method will not be called, and the pre_save and post_save signals will not be sent.

AlvaroLQueiroz added a commit that referenced this issue May 9, 2024
AlvaroLQueiroz added a commit that referenced this issue May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants