I'm attempting to use Django, Ajax, and JSON to retrieve a string, but I'm encountering a problem where the JSON output includes field names instead of just values. Instead of getting only the value of the field, it returns something like [{'field_name': value}] as the complete string. When I check the console, I see: {'field_name': "[{'field_name': value}]"}
Below is the code I have written:
def drugsanddoses(request):
drugIdentifier = request.POST.get('drugID')
drug_group = Antiepileptics.objects.get(name=drugIdentifier)
drug_name = RiskCalculator.objects.filter(drug_name=drug_group).values('drug_name')
l_dose = RiskCalculator.objects.filter(drug_name=drug_group).values('l_dose')
h_dose = RiskCalculator.objects.filter(drug_name=drug_group).values('h_dose')
pubmed_id = RiskCalculator.objects.filter(drug_name=drug_group).values('pubmed_id')
updated = RiskCalculator.objects.filter(drug_name=drug_group).values('updated')
data = {}
try:
data['drug_name'] = str(drug_name)
data['l_dose'] = str(l_dose)
data['h_dose'] = str(h_dose)
data['pubmed_id'] = str(pubmed_id)
data['updated'] = str(updated)
except:
raise Http404
print(data)
return JsonResponse(data, safe=False)