Django: How to use select_related to INNER JOIN FK's FK

killua8p

29 Dec: updated models

I have got three models as follows:

class Job(models.Model):
    job_number = models.CharField(max_length=20, primary_key=True)

class Project(models.Model):
    job = models.ForeignKey(Job, null=True) # updated (null=True)***
    source = models.ForeignKey(Source) # added***

class Task(models.Model):
    project = models.ForeignKey(Project)

class Source(models.Model): # added***
    blahblah...

And I would like to get the job number for a task. Something like below:

job = Job.objects.all().select_related()
jobno = job[0].project.job.job_number

I'm not sure how many times the query above will hit the DB. But I guess it will be more than twice, won't it?

select_related can only pre-cache the foreign key for 2 tables to my understanding. Any one can suggest the best practice in this case to reduce the number of times hitting the DB?

catavaran

select_related() joins all these three models in one query:

>>> from app.models import Task
>>> task = Task.objects.all().select_related()[0]
>>> task.project.job.job_number
u'123'
>>> from django.db import connection
>>> len(connection.queries)
1
>>> connection.queries
[{u'time': u'0.002', u'sql': u'QUERY = u\'SELECT "app_task"."id", "app_task"."project_id", "app_project"."id", "app_project"."job_id", "app_job"."job_number" FROM "app_task" INNER JOIN "app_project" ON ( "app_task"."project_id" = "app_project"."id" ) INNER JOIN "app_job" ON ( "app_project"."job_id" = "app_job"."job_number" ) LIMIT 1\' - PARAMS = ()'}]
>>> 

Readable SQL:

SELECT "app_task"."id", "app_task"."project_id", "app_project"."id",
       "app_project"."job_id", "app_job"."job_number"
FROM "app_task"
INNER JOIN "app_project" ON ( "app_task"."project_id" = "app_project"."id" )
INNER JOIN "app_job" ON ( "app_project"."job_id" = "app_job"."job_number" )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django: How to use select_related to INNER JOIN FK's FK

From Dev

Django select_related FK model

From Dev

How to connect all related DB with FK in Django?

From Dev

Django : How to use select_related for a OneToOneField?

From Dev

How to use select_related with GenericForeignKey in django?

From Dev

Django ModelForms: Seeding a FK related field in the form

From Dev

access django fk related objects in view as template

From Dev

Django:Complex queryset filter for FK related models

From Dev

Django reverse FK query

From Dev

In Django, how to get all instances of a model where an instance of another model related to the first by fk does not exist?

From Dev

how does this select_related work in Django?

From Dev

How to get objects according to FK by using AJAX in django

From Dev

How to get objects according to FK by using AJAX in django

From Dev

Django proper use of select_related or prefetch_related on a ForeignKey

From Dev

Use the PK as FK in JPA/Hibernate

From Dev

Is it possible to use Long FK in activejdbc?

From Dev

Reorient Connectors of FK's in MySQLWorkbench

From Dev

How to update FK to null when deleting optional related entity

From Dev

How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django?

From Dev

TPH - how to satisfy FK constraint when FK is on derived class?

From Dev

Django: Use select_related without a ForeignKey field

From Dev

How to use BigQuery's union all with an inner join?

From Dev

How to make an Inner Join in django?

From Dev

How to perform inner join in django

From Dev

How to use AS in SQL INNER JOIN?

From Dev

How to select_related when using .get() in django?

From Dev

How to select_related when using .get() in django?

From Dev

How to do a Join query using the pivot table and another table using the FK

From Dev

Django Admin - Disable Update For FK Fields

Related Related

  1. 1

    Django: How to use select_related to INNER JOIN FK's FK

  2. 2

    Django select_related FK model

  3. 3

    How to connect all related DB with FK in Django?

  4. 4

    Django : How to use select_related for a OneToOneField?

  5. 5

    How to use select_related with GenericForeignKey in django?

  6. 6

    Django ModelForms: Seeding a FK related field in the form

  7. 7

    access django fk related objects in view as template

  8. 8

    Django:Complex queryset filter for FK related models

  9. 9

    Django reverse FK query

  10. 10

    In Django, how to get all instances of a model where an instance of another model related to the first by fk does not exist?

  11. 11

    how does this select_related work in Django?

  12. 12

    How to get objects according to FK by using AJAX in django

  13. 13

    How to get objects according to FK by using AJAX in django

  14. 14

    Django proper use of select_related or prefetch_related on a ForeignKey

  15. 15

    Use the PK as FK in JPA/Hibernate

  16. 16

    Is it possible to use Long FK in activejdbc?

  17. 17

    Reorient Connectors of FK's in MySQLWorkbench

  18. 18

    How to update FK to null when deleting optional related entity

  19. 19

    How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django?

  20. 20

    TPH - how to satisfy FK constraint when FK is on derived class?

  21. 21

    Django: Use select_related without a ForeignKey field

  22. 22

    How to use BigQuery's union all with an inner join?

  23. 23

    How to make an Inner Join in django?

  24. 24

    How to perform inner join in django

  25. 25

    How to use AS in SQL INNER JOIN?

  26. 26

    How to select_related when using .get() in django?

  27. 27

    How to select_related when using .get() in django?

  28. 28

    How to do a Join query using the pivot table and another table using the FK

  29. 29

    Django Admin - Disable Update For FK Fields

HotTag

Archive