JSON数据将字段名称连接到值字段(Django)

时间:2022-09-21 00:59:04

I'm trying to return a string with JSON, Django and Ajax, however for some reason they queryset is causing the JSON to incorporate the field names as well. For example, instead of returning only the value of the field, it returns the [{'field_name': value}] as an entire string. In console, it prints out: {'field_name': "[{'field_name': value}]"}

我正在尝试使用JSON,Django和Ajax返回一个字符串,但由于某种原因,它们的查询集导致JSON也包含字段名称。例如,它不是仅返回字段的值,而是将[{'field_name':value}]作为整个字符串返回。在控制台中,它打印出来:{'field_name':“[{'field_name':value}]”}

The code I've written is:

我写的代码是:

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)

1 个解决方案

#1


2  

That's the expected behavior, because you are doing str(queryset) on a values() of a query. The values in ORM would return a list of dict. If you only want values, do:

这是预期的行为,因为您正在对查询的values()执行str(queryset)。 ORM中的值将返回dict列表。如果您只想要值,请执行以下操作:

RiskCalculator.objects.filter(drug_name=drug_group) \
                      .values_list('drug_name', flat=True).distinct()

Django doc for values and values_list.

Django doc for values和values_list。

Even with that, it doesn't make sense to do str() on each query, you don't need str() at all. Because you need to serialize the whole python data structure into json. Otherwise you would have a hard time converting a string into a list of values in javascript.

即使这样,在每个查询上执行str()也没有意义,根本不需要str()。因为你需要将整个python数据结构序列化为json。否则,您将很难将字符串转换为javascript中的值列表。

#1


2  

That's the expected behavior, because you are doing str(queryset) on a values() of a query. The values in ORM would return a list of dict. If you only want values, do:

这是预期的行为,因为您正在对查询的values()执行str(queryset)。 ORM中的值将返回dict列表。如果您只想要值,请执行以下操作:

RiskCalculator.objects.filter(drug_name=drug_group) \
                      .values_list('drug_name', flat=True).distinct()

Django doc for values and values_list.

Django doc for values和values_list。

Even with that, it doesn't make sense to do str() on each query, you don't need str() at all. Because you need to serialize the whole python data structure into json. Otherwise you would have a hard time converting a string into a list of values in javascript.

即使这样,在每个查询上执行str()也没有意义,根本不需要str()。因为你需要将整个python数据结构序列化为json。否则,您将很难将字符串转换为javascript中的值列表。