How to use a raw query in Django that uses an external database as well as a Django model

I have an external database with source data. (Microsoft db) How can I query this database and do an inner join on a Django model? I have two connections set up in settings.py This way I can acces the external database like this:

with connection['externalDB'].cursor() as c:

This external db has millions of records. I want run a query and then save that model into another Django model.

SELECT 
    x.product -> External db
    ,d.client_name -> Django model
FROM 
    [external db] x
INNER JOIN 
    [django model] d ON x.client_id = d.client_id
WHERE 
    d.client_name = 'foo.

What would be the best way to approach this?

You can get results from one database and apply this output as a filter for another query on second database You can check Multiple databases

Back to Top