Django prefetch_related From Model With Multiple ManyToMany Relationships

mitchfuku

Say I have a few models in Django:

class Foo(models.Model):
bars = models.ManyToManyField(Bar)
bazs = models.ManyToManyField(Baz)

class Bar(models.Model):
quxs = models.ManyToManyField(Qux)

I can use prefetch_related to get all Bars belonging to Foo and all Quxs belonging to Bar with:

Foo.objects.prefetch_related('bars__quxs')

But how can I use prefetch_related to get this information as well as all the Bazs belonging to Foo? Would something like:

Foo.objects.prefetch_related('bars__quxs', 'bazs')

work?

jproffitt

Yes. You can pass in multiple lookups to .prefetch_related()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Joining ManyToMany fields with prefetch_related in Django

From Dev

prefetch_related with Django 1.5 + django-model-utils

From Dev

Django: how to filter a model with ManyToMany relationships

From Dev

Django prefetch_related GenericForeignKey with multiple content types

From Dev

Django 1.9.1 prefetch_related with only hit multiple times to the database

From Dev

Django prefetch_related GenericForeignKey with multiple content types

From Dev

Django prefetch_related - filter with or-clause from different tables

From Dev

Django prefetch_related from foreignkey with manytomanyfield not working

From Dev

Django prefetch_related from foreignkey with manytomanyfield not working

From Dev

prefetch_related for multiple Levels

From Dev

Django prefetch_related and select_related

From Dev

How do we filter results from two tables using prefetch_related function in Django

From Dev

Django/python: Model relationships for API related data

From Dev

Uniquely identifying ManyToMany relationships in Django

From Dev

Is it possible to prefetch_related over multiple levels?

From Dev

django prefetch_related id only

From Dev

Filter prefetch_related empty in django

From Dev

Is django prefetch_related supposed to work with GenericRelation

From Dev

django prefetch_related id only

From Dev

Django, general version of prefetch_related()?

From Dev

When splitting a Django model, How to keep ForeignKey and ManyToMany relationships during data migration?

From Dev

Complex model relationships in django

From Dev

django- Use prefetch_related inside of another prefetch_related

From Dev

Django proper use of select_related or prefetch_related on a ForeignKey

From Java

django rest framework - backward serialization to avoid prefetch_related

From Dev

How to use prefetch_related with Django's DetailView

From Dev

Django prefetch_related with m2m through relationship

From Dev

Django avoid extra queries using prefetch_related not working

From Dev

Could not figure out the use of prefetch_related in django

Related Related

  1. 1

    Joining ManyToMany fields with prefetch_related in Django

  2. 2

    prefetch_related with Django 1.5 + django-model-utils

  3. 3

    Django: how to filter a model with ManyToMany relationships

  4. 4

    Django prefetch_related GenericForeignKey with multiple content types

  5. 5

    Django 1.9.1 prefetch_related with only hit multiple times to the database

  6. 6

    Django prefetch_related GenericForeignKey with multiple content types

  7. 7

    Django prefetch_related - filter with or-clause from different tables

  8. 8

    Django prefetch_related from foreignkey with manytomanyfield not working

  9. 9

    Django prefetch_related from foreignkey with manytomanyfield not working

  10. 10

    prefetch_related for multiple Levels

  11. 11

    Django prefetch_related and select_related

  12. 12

    How do we filter results from two tables using prefetch_related function in Django

  13. 13

    Django/python: Model relationships for API related data

  14. 14

    Uniquely identifying ManyToMany relationships in Django

  15. 15

    Is it possible to prefetch_related over multiple levels?

  16. 16

    django prefetch_related id only

  17. 17

    Filter prefetch_related empty in django

  18. 18

    Is django prefetch_related supposed to work with GenericRelation

  19. 19

    django prefetch_related id only

  20. 20

    Django, general version of prefetch_related()?

  21. 21

    When splitting a Django model, How to keep ForeignKey and ManyToMany relationships during data migration?

  22. 22

    Complex model relationships in django

  23. 23

    django- Use prefetch_related inside of another prefetch_related

  24. 24

    Django proper use of select_related or prefetch_related on a ForeignKey

  25. 25

    django rest framework - backward serialization to avoid prefetch_related

  26. 26

    How to use prefetch_related with Django's DetailView

  27. 27

    Django prefetch_related with m2m through relationship

  28. 28

    Django avoid extra queries using prefetch_related not working

  29. 29

    Could not figure out the use of prefetch_related in django

HotTag

Archive