0

I am trying to get the choice labels from a field that stores an array of choices - I am able to get the label when the field has a single value.

Here is the model:

class TobaccoUse(models.Model):
    tu_id = models.AutoField(primary_key=True)
    patient_id_fk = models.ForeignKey('patient_management.PatientInformation', on_delete=models.CASCADE)
    SMOKING_STATUS_CHOICES = [
        ('N', 'Never Smoked'),
        ('P', 'Past Smoker'),
        ('C', 'Current Smoker')
    ]
    tu_smoking_status = models.CharField(max_length=1, choices=SMOKING_STATUS_CHOICES)
    OTHER_USE_CHOICES = [
        ('P', 'Pipe'),
        ('C', 'Cigar'),
        ('S', 'Snuff'),
        ('CH', 'Chew')
    ]
    tu_other_use = ArrayField(models.CharField(max_length=2, choices=OTHER_USE_CHOICES, null=True, blank=True), size=4, default=list, null=True, blank=True)
    tu_packs_per_day = models.DecimalField(blank=True, null=True, max_digits=4, decimal_places=1)
    tu_years_smoked = models.PositiveSmallIntegerField(blank=True, null=True)
    tu_quit_date = models.DateField(blank=True, null=True)
    tu_is_active = models.BooleanField(default=True)

The view:

class ListTobaccoUseOptions(APIView):
    def get(self, request):
        statusChoices = []
        otherChoices = []
        for ssc in TobaccoUse.SMOKING_STATUS_CHOICES:
            statusChoices.append({'label': ssc[1], 'value': ssc[0]})
        for soc in TobaccoUse.OTHER_USE_CHOICES:
            otherChoices.append({'label': soc[1], 'value': soc[0]})
        try:
            smoking_status_categories = {'status_categories': statusChoices, 'other_use_categories': otherChoices}
            return Response(smoking_status_categories, status=status.HTTP_200_OK)
        except Exception as e:
            return Response(data={'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)

And the serializers:

class PatientMedicalHxSerializer(serializers.ModelSerializer):
    prognosis = serializers.SerializerMethodField()
    primary_diagnosis = serializers.SerializerMethodField()
    surgical_diagnoses = serializers.SerializerMethodField()
    secondary_diagnoses = serializers.SerializerMethodField()
    wellness_screenings = serializers.SerializerMethodField()
    tobacco_use = serializers.SerializerMethodField()
    vape_use = serializers.SerializerMethodField()

    class Meta:
        model = PatientInformation
        fields = ('primary_diagnosis', 'secondary_diagnoses', 'surgical_diagnoses', 'prognosis', 'wellness_screenings', 'tobacco_use', 'vape_use')

    def get_primary_diagnosis(self, patientinformation):
        qs = PatientToPrimaryDiagnosis.objects.filter(patient_id_fk = patientinformation, ptpd_is_active = True)
        serializer = PatientToPrimaryDiagnosisSerializer(instance = qs, many=True)
        return serializer.data
    
    def get_secondary_diagnoses(self, patientinformation):
        qs = PatientToSecondaryDiagnoses.objects.filter(patient_id_fk = patientinformation, ptsd_is_active = True)
        serializer = GetPatientToSecondaryDiagnosesSerializer(instance = qs, many=True)
        return serializer.data
    
    def get_surgical_diagnoses(self, patientinformation):
        qs = PatientToPCS.objects.filter(patient_id_fk = patientinformation, ptpcs_is_active = True)
        serializer = GetPatientSurgicalDiagnosisSerializer(instance = qs, many=True)
        return serializer.data
    
    def get_prognosis(self, patientinformation):
        qs = PatientToPrognosis.objects.filter(patient_id_fk = patientinformation, ptp_is_active = True)
        serializer = GetPatientPrognosisSerializer(instance = qs, many=True)
        return serializer.data
    
    def get_wellness_screenings(self, patientinformation):
        qs = PatientToWellnessScreenings.objects.filter(patient_id_fk = patientinformation, ws_is_active = True)
        serializer = WellnessScreeningsSerializer(instance = qs, many=True)
        return serializer.data
    
    def get_tobacco_use(self, patientinformation):
        qs = TobaccoUse.objects.filter(patient_id_fk = patientinformation, tu_is_active = True)
        serializer = TobaccoUseSerializer(instance = qs, many=True)
        return serializer.data
    
    def get_vape_use(self, patientinformation):
        qs = VapeUse.objects.filter(patient_id_fk = patientinformation, vu_is_active = True)
        serializer = VapeUseSerializer(instance = qs, many=True)
        return serializer.data

And here is the Tobacco Use serializer:

class TobaccoUseSerializer(serializers.ModelSerializer):
    status = serializers.CharField(source = 'get_tu_smoking_status_display')
    class Meta:
        model = TobaccoUse
        fields = ('__all__')    

I tried to use the get_field_display on the tu_other_use field, but it gives me an error. I'm assuming that somehow I have to loop through all of the values in the array to get the display, but I haven't figured out how to make it work.

Here is the return result so far:

[
    {
        "primary_diagnosis": [],
        "secondary_diagnoses": [],
        "surgical_diagnoses": [],
        "prognosis": [],
        "wellness_screenings": [],
        "tobacco_use": [
            {
                "tu_id": 11,
                "status": "Past Smoker",
                "tu_smoking_status": "P",
                "tu_other_use": [
                    "C"
                ],
                "tu_packs_per_day": "2.5",
                "tu_years_smoked": 8,
                "tu_quit_date": null,
                "tu_is_active": true,
                "patient_id_fk": 70
            }
        ],
        "vape_use": []
    }
]

I'd like to have a return field that has something like "other": ["Cigar", etc.]

Thanks!

4
  • Can you clarify the question a little? The view does not appear to use any of the serializers, for example - alternatively, you could also consider removing anything that is not needed for the question. But if I understand you correctly, does the question boil down to making tu_other_use contain ["Cigar",] instead of ["C",]? Commented Jan 6 at 6:37
  • Why do you make all these PatientToPrognosis.objects.filter(..) queries in the serializer? It will likely slow down the API enormously by not doing this in bulk. Commented Jan 6 at 8:47
  • @Vegard - yes, that is basically what I would like to happen. Commented Jan 6 at 15:56
  • @willeM_VanOnsem - I plan on refining this in the future, I am still pretty new to Django and DRF Commented Jan 6 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.