Consultation of two Models in Django

1

I have these two models:

class Pacientes(models.Model):
nombres = models.CharField(max_length=50)
apellidos = models.CharField(max_length=50)
numero_identificacion = models.CharField(max_length=10)
fecha_nacimiento = models.DateField(blank=False,)
celular = models.CharField(max_length=10)
email = models.CharField(max_length=30)
direccion = models.CharField(max_length=100)
genero = models.CharField(max_length=1, choices=PACIENTE_SEXO)



class Citas(models.Model):
estado = models.CharField(max_length=10, choices=ESTADOS_CITA, 
default=ESTADO_INGRESADO)
laboratorio = models.CharField(max_length=10, choices=LABORATORIO_TODOS, 
null=True)
idexamen = models.CharField(max_length=50, choices=EXAMENES_TODOS)
idpaciente = models.ForeignKey(Pacientes, on_delete=models.CASCADE)
doctor = models.CharField(max_length=30, choices=DOCTORES_TODOS)

What I want to do is a query that in SQL would be:

select c.estado, c.laboratorio, c.idexamen, p.nombres, p.apellidos, c.doctor
from pacientes p, citas c
where c.idpaciente = p.id

Someone can give me help. Thank you.

    
asked by Roberto Feijoo 05.07.2018 в 22:03
source

3 answers

1

Thanks to Django, this is not complicated, I'm going to assume that the id = 1 of the patient exists:

# Observación: para el nombre de los modelos se usan palabras en singular, los fields con relaciones se suele poner con el nombre del modelo por ejemplo: en vez de idpaciente pon simplemente paciente
try:
   paciente = Pacientes.objects.get(id=1)
   citas = Citas.objects.filter(idpaciente=paciente)
   for c in citas:
       print("{0} {1} {2} {3} {4} {5} {6}".format(c.estado, c.laboratorio, c.idexamen, c.idpaciente.nombres, c.idpaciente.apellidos, c.idpaciente.doctor))

the filter of the appointments of a patient given an id can be done in two ways that I have shown you previous lines and I show you now:

citas = Citas.objects.filter(idpaciente__id=1)

as idpaciente is a patient FK with the double underscore we can refer to any of its attributes from Appointments

    
answered by 06.07.2018 / 13:48
source
0

First you must reference the relationship in the model, in this case I infer that it will be OneToMany, so you should put a "patient" field in "appointment", something like "patient = models.ForeignKey (Patient). will make the relationship in the database by itself.

When it comes to recovering the data that you think is appropriate, you should use the method that best suits your needs. Django provides several methods in its documentation link . It depends on if you need a list, a QuerySet, a SingleResult, etc. It may seem difficult to first but seeing the API you will understand it quickly (of course, giving you a couple of milks).

In this case it would be worth doing a "patient_cites = Citas.objects.filter (patient_id = patient.id)" Once the query is done, you can access the data through the variable "patient_cites".

I hope this helps you understand this a bit more. Do not forget to consult the documentation to clarify this type of doubts.

P.D. I have been working with django for 4 months, so I hope I have not messed up more than you already were.

    
answered by 06.07.2018 в 11:26
0

Try the raw method to execute custom SQL queries in Django

resultado = Paciente.objects.raw('select c.estado, c.laboratorio, c.idexamen, 
p.nombres, p.apellidos, c.doctor
from pacientes p, citas c
where c.idpaciente = p.id')
    
answered by 13.11.2018 в 23:13