Django - Join two Table without Foreign key

2024/10/10 10:22:40

I have two tables and want to join them.. but I can't do that without rawQueryset and raw SQL.

how can i join two models without foreign key? The columns for JOIN is not unique so it can't be PK and Foreign Key.

I want to get the SQL LIKE THIS

'SELECT * FROM genome AS A JOIN metadata AS B ON A.query_id = B.sample_id',

and this the models I used.


class Genome(models.Model):query_id = models.CharField(max_length=100) ref_id = models.CharField(max_length=30)matching_hashes = models.CharField(max_length=30)

class Metadata(models.Model):project_id = models.CharField(max_length=50) # Metagenome의 query id와 JOIN함sample_id = models.CharField(max_length=50)
Answer

You can try this:

Firstly, construct a SQL query as desired

sql_query = "SELECT * FROM genome AS A JOIN metadata AS B ON A.query_id = B.sample_id"

User that SQL query in django DB connection like following:

from django.db import connectiondef my_custom_sql(self):cursor = connection.cursor()    cursor.execute(sql_query)row = cursor.fetchall()return row

Or you can try to execute raw() as following

Genome.objects.raw(sql_query) # in some cases it may not work
https://en.xdnf.cn/q/118468.html

Related Q&A

Understanding lambda functions

Well I did try to read about Lambda functions but did not get across any link which explains few questions about its flow and the way it is handled by python interpretor or may be I could not understan…

JSON to Python dataframe: mapping values from another API

I have an API with student data like this, for every student id there will be a corresponding API link with mark details. for example: https://api.school.com/2020/students.json {"Students": […

Create a cycle out of scattered points

I know this sounds trivial, but my head is refusing to give an algorithm for this.I have a bunch of points scattered on a 2-D plane and want to store them in a list such that they create a ring. The po…

Python Dictionary w/ 2 Keys?

Can I have a python dictionary with 2 same keys, but with different elements?

Tkinter throwing a KeyError when trying to change frames

Im learning tkinter off of the Sentdex tutorials and I into a problem when trying to change pages. My compiler throws something about a KeyError that it doesnt give whenever I change the button on the …

How to send messages to other clients only in the sequence of adding clients?

https://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/sever.pyhttps://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/client.pyWhen server starts, and suppose three clients …

Dictionary keeps getting overwritten in each iteration of for-loop

import randomo=[,,!,@,#,$,%,^,&,*,(,),,_,=,+,/,[] q=[1,2,3,4,5,6,7,8,9,0]for i in top_25:wordDic ={i: random.choice(o)+random.choice(q)} print(wordDic)(top_25 is an array of words, and the random.c…

Python socket communication with HP print server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question appears to be off-topic because it lacks sufficient information to diagnose the proble…

Why are torch.version.cuda and deviceQuery reporting different versions?

I have a doubt about the CUDA version installed on my system and being effectively used by my software. I have done some research online but could not find a solution to my doubt. The issue which helpe…

How to have 2 advertisements in BLE(BlueTooth Low Energy)?

Im working on BLE advertisement. Id like to know if its possible to have 2 advertisements in BLE. I need to have both service data and manufacturer data. Im using Python. The code is based on https://g…