close

How to make an Inner Join in django?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to make an Inner Join in django in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to make an Inner Join in django?

  1. How to make an Inner Join in django?

    InnerJoin implies the “common” section between the two (or more) tables. Each one being performed one after the other, as your SQL query suggests.

  2. make an Inner Join in django

    InnerJoin implies the “common” section between the two (or more) tables. Each one being performed one after the other, as your SQL query suggests.

Method 1

You are probably looking for select_related, which is the natural way to achieve this:

pubs = publication.objects.select_related('country', 'country_state', 'city')

You can check the resulting SQL via str(pubs.query), which should result in output along the following lines (the example is from a postgres backend):

SELECT "publication"."id", "publication"."title", ..., "country"."country_name", ...  
FROM "publication" 
INNER JOIN "country" ON ( "publication"."country_id" = "country"."id" ) 
INNER JOIN "countrystate" ON ( "publication"."countrystate_id" = "countrystate"."id" ) 
INNER JOIN "city" ON ( "publication"."city_id" = "city"."id" ) 

The returned cursor values are then translated into the appropriate ORM model instances, so that when you loop over these publications, you access the related tables’ values via their own objects. However, these accesses along the pre-selected forward relations will not cause extra db hits:

{% for p in pubs %}
     {{ p.city.city_name}}  # p.city has been populated in the initial query
     # ...
{% endfor %}

Method 2

Let me beginning by describing meanings of terms and get order going…

InnerJoin implies the “common” section between the two (or more) tables. Each one being performed one after the other, as your SQL query suggests.

going through your SQL query, you are looking at Publication as the main, all your query are foreign key within publication, giving you ENTIRE dataset.

if my understanding goes correctly what you are looking for in Django equivalent is filter, in chain (not Query), because Q will give you results from each Q separately and join them, while you want results from one applied onto another.

(I dont know what the dic.update({“plist”: mypublications }) does, its not clear.. Solution: country = country.objects.all() # gets all the country in the table of country. country_state = countrystate.objects.all() # get all the countrystate objects city = city.objects.all() # get all city objects

solution 1: in python3 you need to use __in, this works ok in Python2. and will get you country with anything from table country (not None), table countrystate (not None), table city (not None), so if anything from anyone of them, it would provide. Note: ‘None’ (python) = ‘Null’ (SQL Database) Getting all publications that are having anything from the above (practically will bring you anything. publications_list = publication.objects.filter(country = country, countrystate = country_state, city = city) # get if anyone from any of those where ‘id’ (based on which is object) match, which makes it innerjoin in itself.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read