Hi,
I need to get information from 3 different tables but I don’t know how to do it.
Here are my models:
class Alunos(models.Model):
class Meta:
verbose_name_plural = 'Alunos'
nome = models.CharField(max_length=50, help_text="Nome do aluno", verbose_name="Nome:")
dtnasc = models.DateField(help_text="Data de Nascimento", verbose_name="Data de Nascimento:")
alunoid = models.CharField(max_length=4, help_text="ID interno do aluno", verbose_name="ID Aluno:")
classeid = models.ForeignKey(Turmas, on_delete=models.CASCADE, verbose_name="Classe:")
educaid = models.ForeignKey(Educa, on_delete=models.CASCADE, verbose_name="Encarregado de Educação:")
moradaid = models.ForeignKey(Morada, on_delete=models.CASCADE, verbose_name="Morada:")
cpostalid = models.ForeignKey(CodPostal, on_delete=models.CASCADE, verbose_name="Código Postal:")
ativo = models.BooleanField(default=True)
notas = models.TextField(help_text="Notas importantes")
def __str__(self):
return self.nome
class Turmas(models.Model):
class Meta:
verbose_name_plural = 'Turmas'
turma_name = models.CharField(max_length=50, help_text="Nome da classe", verbose_name="Classe:")
weekdays = models.CharField(max_length=7, choices=weekdays_choices, help_text="Dias da classe", verbose_name="Dias:")
times = models.CharField(max_length=20, choices=class_times, help_text="Horário da classe", verbose_name="Horas:")
profid = models.ForeignKey(Professores, on_delete=models.CASCADE, verbose_name="Professor:")
anoid = models.ForeignKey(Ano, on_delete=models.CASCADE, verbose_name="Ano:")
def __str__(self):
return self.turma_name
class Educa(models.Model):
class Meta:
verbose_name_plural = 'Encarregados de Educação'
nome_encarregado = models.CharField(max_length=150, help_text="Nome do encarregado de educação")
nif = models.CharField(max_length=9, help_text="NIF do encarregado de educação ou aluno")
email = models.EmailField(max_length=100, help_text="e-mail do encarregado de educação")
telefone = models.CharField(max_length=9, help_text="Telefone do encarregado de educação")
pagamento = models.CharField(max_length=11, choices=pagamentos_choices, help_text="Último pagamento")
def __str__(self):
return self.nome_encarregado
This is the SQL I need to transform into ORM in the view
select c.turma_name, a.nome, a.alunoid, a.educaid_id, b.nif, b.email, b.telefone
from schooladmin_alunos a, schooladmin_turmas c, schooladmin_educa b
where a.id = c.id and a.id = b.id
Any help would be much appreciated.
Thanks in advance