Optmized Query

27 views
Skip to first unread message

Soumen Khatua

unread,
May 27, 2020, 7:22:44 AM5/27/20
to django...@googlegroups.com
Hi Folks,
I have many to many relationships and Foreign Key in the table, I'm using select_realted(foreign key filed name) to optimize the query but I want to fetch many to many and foreign key at the same time , How I can do this in very optimized way?

Thank You
Regards,
Soumen

Chetan Ganji

unread,
May 27, 2020, 9:51:23 AM5/27/20
to django...@googlegroups.com
select_related for fk and prefetch_related for m2m in django, you can chain them together 

Regards,
Chetan Ganji
+91-900-483-4183


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6Wb6f-BCwUZfvgzGtsrbV1seq1iGbXyuqoH%3DKxZrJ2EyLg%40mail.gmail.com.

Soumen Khatua

unread,
May 27, 2020, 4:32:22 PM5/27/20
to django...@googlegroups.com
I also know about this concept but I don't how I can achieve it, Could you give me an example?
Suppose I have:

class Profile(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete = models.CASCADE,
        related_name="profile"
    )
    location = models.ManyToManyField(Location)



Thank you

Regards,
Soumen

Chetan Ganji

unread,
May 27, 2020, 4:43:16 PM5/27/20
to django...@googlegroups.com
Profile.objects.filter().select_related("user").prefetch_related("location")

Soumen Khatua

unread,
May 27, 2020, 5:25:20 PM5/27/20
to django...@googlegroups.com
Now I'm facing one problem whenever I'm trying to iterate a loop I'm getting too many SQL variables. Actually after iterating the loop I want to render all the value in the template.
if could you resolve this it would be very good for me.
Thank you for your time and response

Regards,
Soumen


Soumen Khatua

unread,
May 27, 2020, 5:43:08 PM5/27/20
to django...@googlegroups.com
Actually I want to fetch all users from User table and users location from Location table which is many to many relation from same Profile table. Is it possible??? 

אורי

unread,
May 29, 2020, 12:32:43 AM5/29/20
to django...@googlegroups.com
Hi Soumen,

Sometimes prefetch_related is much faster and more efficient than select_related. I usually prefer to use prefetch_related.

Uri.


Sencer Hamarat

unread,
May 29, 2020, 5:48:29 AM5/29/20
to django...@googlegroups.com
I thought select_related works for only FK/M2M and prefetch_related works for only reverse relations, doesn't it?

Saygılarımla,
Sencer HAMARAT



אורי

unread,
May 29, 2020, 8:20:30 AM5/29/20
to django...@googlegroups.com

Soumen Khatua

unread,
May 29, 2020, 10:42:35 AM5/29/20
to django...@googlegroups.com
Hi,

Yeah, but whenever I'm trying to iterate a loop on all these qs. I'm getting an error too many sql variables. I know I'm doing something wrong,but I'm not able to find the error.


Thank you for your response and time.

Regards,
Soumen

Reply all
Reply to author
Forward
0 new messages