Sourabh Sharma
Sourabh Sharma

Reputation: 9

How do I design my django app which fetches the data from a legacy database which has huge number of tables?

I am developing a Django app which fetches the data from a huge database and then perform some statistical operation and prints the result. There are different ways available but I want to know what is the best way of doing it?

  1. Dataset have a lot of tables, so is it advisable to create models for all those tables? and use Django ORM to replicate below query?

  2. OR else I should not use Models at all and use RAW SQL query to fetch the data from database and perform operation and then show the result. If yes then please suggest the best way of doing it.

"USE r_d;
SELECT nus.noyau_utilisateur_id as "candidate_id", 
pcon.id as "assessment_id", 
pcon.created_at as "assessment_creation_date", 
YEAR(pcon.created_at) as "assessment_year",
tti.libelle as "test_name", 
tfi.libelle as "factor_name", 
pcal.score_reel as "real_score", 
pcal.score_brut as "raw_score", 
pcal.score_calcule as "calculated_score"
FROM `passation_calcul` pcal
JOIN `passation_contrat` pcon ON pcon.id = pcal.passation_contrat_id
JOIN `noyau_utilisateur_societe` nus ON nus.id = pcon.noyau_utilisateur_societe_id
JOIN `test_test` tt ON tt.id = pcon.test_test_id
JOIN `test_test_i18n` tti ON tt.id = tti.test_test_id
JOIN `test_facteur` tf ON tf.id = pcal.id_ref
JOIN `test_facteur_i18n` tfi ON tf.id = tfi.test_facteur_id
WHERE pcon.test_test_id = 144
AND pcon.etat = "COMPLETED"
AND tti.noyau_ref_langue_produit_id = 1
AND pcal.table_ref = 'test_facteur'
AND tfi.noyau_ref_langue_produit_id = 1
ORDER BY candidate_id, assessment_id, factor_name"```

Upvotes: 0

Views: 268

Answers (2)

Shahzan Sadick
Shahzan Sadick

Reputation: 306

After copying the output to models.py.. make the required changes if any.

python manage.pymakemigrations

Now we need to migrate, that will create the required tables in the db in accordance to the models in model.py.

But we already have those tables.. that's where fake migration comes in..

python manage.py migrate --fake

Before starting read this.. and understand what you are doing.. this will enable you to make django orm queries without using SQL seamlessly.

https://docs.djangoproject.com/en/4.0/ref/django-admin/#migrate

Upvotes: 0

Shahzan Sadick
Shahzan Sadick

Reputation: 306

I think you, after specifying the database settings to point to your legacy db in the settings.py, you should run

python manage.py inspectdb

that will generate python model classes of the existing table isn the db.. write the output to models.py and chek it out

This might help. more detailed.

https://django-book.readthedocs.io/en/latest/chapter18.html

Upvotes: 1

Related Questions