[Answer]-Django slow at creating objects?

1๐Ÿ‘

โœ…

If you install Django debugtoolbar you could actually see the queries being fired, and the code responsible on firing them.

This slowness is mainly because django fires a new db query every time you do e.mobile and e.contact_owner, in the loop.

To prevent these queries, prefetch the data using select_related like below

query = Contact.objects.select_related('mobile', 'contact_owner').filter(contact_owner=batch.user, subscribed=True)

If your relations are many to many, then use prefetch_related rather than select_related.

๐Ÿ‘คMeitham

Leave a comment