I got stuck a bit with a small problem when doing my school project, the problem is that I have an api with DRF and wanting to show my patient data "main table" shows them without problems but when I want to show other patient data in a different table (this table is this reference with a foreign key to Patient) I have not managed to obtain the patient data from this other table.
I can not make my api send me the other patient data from the foreign key referenced to the patient, could you help me?
models.py
class Paciente(TimeStampedModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
udi = models.UUIDField(default=uuid.uuid4, editable=False)
first_name = models.CharField('Nombre(s)', max_length=100)
last_name = models.CharField('Apellidos', max_length=100)
gender = models.CharField('Sexo', max_length=20, choices=GENDER_CHOICES)
birth_day = models.DateField('Fecha de nacimiento', blank=True, null=True)
phone_number = models.CharField('Número de telefono', max_length=13)
civil_status = models.CharField('Estado civil', max_length=20, choices=CIVIL_STATUS_CHOICES)
etc.....
class Antecedentes(TimeStampedModel):
"""
Modelo de motivo y antecedentes de la enfermedad presentada en el momento de
la consulta
"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
paciente = models.ForeignKey(Paciente, on_delete=models.CASCADE, null=True)
motivo = models.TextField('Motivo de la consulta')
antecedentes = models.TextField('Antecedentes de la enfermedad actual', blank=True, null=True)
serializers.py
class antecedenteSerializer(serializers.ModelSerializer):
user = serializers.ReadOnlyField(source="user.username")
class Meta:
model = Antecedentes
fields = ('paciente' ,'motivo', 'antecedentes', )
views.py I was trying this but I'm not if this is correct or not
from historiaClinica import models as modelsHC
class antecedenteList(APIView):
"""
Lista todos los antecedentes o crea uno nuevo
"""
def get_object(self, pk):
try:
paciente = get_object_or_404(pk=pk)
return modelsHC.Antecedentes.objects.get(paciente=paciente)
except modelsHC.Antecedentes.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
antecedente = self.get_object(pk)
serializer = antecedenteSerializer(antecedente)
return Response(serializer.data)
If you need to show all Antecedentes
related to the specific Paciente
you can use reverse lookup paciente.antecedentes_set.all()
so in view you can do this:
class antecedenteList(APIView):
"""
Lista todos los antecedentes o crea uno nuevo
"""
def get_object(self, pk):
try:
paciente = get_object_or_404(pk=pk)
return paciente
except modelsHC.Antecedentes.DoesNotExist:
raise Http404
def get(self, request, pk, format=None):
paciente = self.get_object(pk)
antecedentes = paciente.antecedentes_set.all()
serializer = antecedenteSerializer(antecedentes, many=True)
return Response(serializer.data)
Note I am using serializer's many=True
argument to serialize multipe objects at the same time.
Also you may need in the future nested serialization to show all antecedentes
in the paciente
data. Details of nested serialization you can find here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.