django prefetch_related id only

demux

I'm trying to optimise my queries but prefetch_related insists on joining the tables and selecting all the fields even though I only need the list of ids from the relations table.

queries

You can ignore the 4th query. It's not related to the question.

Related Code:

class Contact(models.Model):
    ...
    Groups = models.ManyToManyField(ContactGroup, related_name='contacts')
    ...

queryset = Contact.objects.all().prefetch_related('Groups')
Freaky Dug

Django 1.7 added Prefetch objects which let you customise the queryset used when prefetching. In this case, you'd want something like:

queryset = Contact.objects.all().prefetch_related(
    Prefetch('Groups', queryset=Group.objects.all().only('id')))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What's the difference between select_related and prefetch_related in Django ORM?

From Java

django rest framework - backward serialization to avoid prefetch_related

From Dev

Django: check if value in values_list with & without prefetch_related/select_related

From Dev

Django prefetch_related From Model With Multiple ManyToMany Relationships

From Dev

django prefetch_related id only

From Dev

Django proper use of select_related or prefetch_related on a ForeignKey

From Dev

using prefetch_related to get only a particular entry of a relation

From Dev

Is django prefetch_related supposed to work with GenericRelation

From Dev

How to use prefetch_related with Django's DetailView

From Dev

Django prefetch_related from foreignkey with manytomanyfield not working

From Dev

Joining ManyToMany fields with prefetch_related in Django

From Dev

Django avoid extra queries using prefetch_related not working

From Dev

Django prefetch_related GenericForeignKey with multiple content types

From Dev

Filter prefetch_related empty in django

From Dev

Django prefetch_related with m2m through relationship

From Dev

Django 1.9.1 prefetch_related with only hit multiple times to the database

From Dev

Django prefetch_related - filter with or-clause from different tables

From Dev

django- Use prefetch_related inside of another prefetch_related

From Dev

Could not figure out the use of prefetch_related in django

From Dev

django rest framework - backward serialization to avoid prefetch_related

From Dev

prefetch_related with Django 1.5 + django-model-utils

From Dev

Django, general version of prefetch_related()?

From Dev

How to use prefetch_related with Django's DetailView

From Dev

Django prefetch_related from foreignkey with manytomanyfield not working

From Dev

Django prefetch_related GenericForeignKey with multiple content types

From Dev

Could not figure out the use of prefetch_related in django

From Dev

How to clear Django's cached query after prefetch_related

From Dev

Django prefetch_related and select_related

From Dev

django select_related vs prefetch_related in tree like table

Related Related

  1. 1

    What's the difference between select_related and prefetch_related in Django ORM?

  2. 2

    django rest framework - backward serialization to avoid prefetch_related

  3. 3

    Django: check if value in values_list with & without prefetch_related/select_related

  4. 4

    Django prefetch_related From Model With Multiple ManyToMany Relationships

  5. 5

    django prefetch_related id only

  6. 6

    Django proper use of select_related or prefetch_related on a ForeignKey

  7. 7

    using prefetch_related to get only a particular entry of a relation

  8. 8

    Is django prefetch_related supposed to work with GenericRelation

  9. 9

    How to use prefetch_related with Django's DetailView

  10. 10

    Django prefetch_related from foreignkey with manytomanyfield not working

  11. 11

    Joining ManyToMany fields with prefetch_related in Django

  12. 12

    Django avoid extra queries using prefetch_related not working

  13. 13

    Django prefetch_related GenericForeignKey with multiple content types

  14. 14

    Filter prefetch_related empty in django

  15. 15

    Django prefetch_related with m2m through relationship

  16. 16

    Django 1.9.1 prefetch_related with only hit multiple times to the database

  17. 17

    Django prefetch_related - filter with or-clause from different tables

  18. 18

    django- Use prefetch_related inside of another prefetch_related

  19. 19

    Could not figure out the use of prefetch_related in django

  20. 20

    django rest framework - backward serialization to avoid prefetch_related

  21. 21

    prefetch_related with Django 1.5 + django-model-utils

  22. 22

    Django, general version of prefetch_related()?

  23. 23

    How to use prefetch_related with Django's DetailView

  24. 24

    Django prefetch_related from foreignkey with manytomanyfield not working

  25. 25

    Django prefetch_related GenericForeignKey with multiple content types

  26. 26

    Could not figure out the use of prefetch_related in django

  27. 27

    How to clear Django's cached query after prefetch_related

  28. 28

    Django prefetch_related and select_related

  29. 29

    django select_related vs prefetch_related in tree like table

HotTag

Archive