Doubt on converting SQL to Django ORM

Asked

Viewed 35 times

0

Hello!

I am studying starting in Django ORM and would like help to convert the SQL below as I am not able to perform the necessary junctions. Follows:

SELECT P.nome, T.nome FROM 
professor as D 
INNER JOIN pessoa_fisica AS PF ON (D.pessoa_fisica_id = PF.pessoa_id) 
INNER JOIN pessoa AS P ON (PF.pessoa_id = P.id)
INNER JOIN funcionario AS F ON(F.pessoafisica_id = PF.pessoa_ptr_id) 
INNER JOIN ocupacao AS S ON(S.funcionario_id = F.pessoafisica_id)
INNER JOIN Titulo as T on (S.titulacao_id = T.id) ORDER BY p.nome 

What is the best way to accomplish this in Django ORM?

Thank you!

  • Welcome. Post here what you have tried so far.

  • Hello, Leonardo! Thank you for your reply. Follow one of my last attempts: qs = Professor.objects.filter(pessoa_fisica__pessoa_funcionario_ocupacao__titulo)

NameError: name 'pessoa_fisica__pessoa__funcionario__ocupacao__titulo' is not defined


  • Hello, Leonardo! I came to a conclusion, in conversations with other people. A strategy with values_list was used. Even so I follow with some doubts and confusion with the Django ORM... I believe that the time will decrease these questions. Follows the solution to my case: qs = Professor.objects.all().values_list('pessoa_fisica__nome','pessoa_fisica__funcionario__ocupacao__titulo__nome')

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.