Django视图代码有奇怪的行为

时间:2015-12-16 03:38:18

标签: python django

我写了这个Django视图方法:

def list_all_devices(request):

    all_devices_info = []
    fields = Devices.objects.all()
    for field in fields:
        device = Devices.objects.filter(device_id=field.device_id).values()
        dev = device[0]

        dev['variables'] = Variables.objects.filter(device_id=field.device_id).values()

        del dev['device_creation_datetime']
        all_devices_info.append(dev)

    return HttpResponse(json.dumps(all_devices_info), content_type="application/json")

由于某种原因,第一个代码不起作用,但如果我做了如下的小修改,它就会开始工作,我无法弄清楚原因。

def list_all_devices(request):

    all_devices_info = []
    fields = Devices.objects.all()
    for field in fields:
        device = Devices.objects.filter(device_id=field.device_id).values()
        dev = device[0]

        dev['variables'] = [v for v in Variables.objects.filter(device_id=field.device_id).values()]

        del dev['device_creation_datetime']
        all_devices_info.append(dev)

    return HttpResponse(json.dumps(all_devices_info), content_type="application/json")

当我分析修改时:

[v for v in Variables.objects.filter(device_id=field.device_id).values()]

Variables.objects.filter(device_id=field.device_id).values()
它们似乎很相似,似乎都是列表,内部也有相同的信息。但是当调用json.dumps方法时,在第一个代码中它会在浏览器上抛出以下错误。

enter image description here

2 个答案:

答案 0 :(得分:1)

values()不会返回简单的数据类型。

我尝试使用django模型:

dir(myModel.objects.all().values())

[&#39; &#39;,&#39; bool &#39;,&#39; &#39;,&#39; deepcopy &#39;,&#39; delattr &#39;,&#39; dict &#39;,&#39; doc &#39;,&#39; 格式&#39;,&#39; getattribute &#39;,&#39; getitem &#39;,&#39; getstate &#39;,&#39; 哈希< / strong>&#39;,&#39; init &#39;,&#39; iter &#39;,&#39; len &#39;,&#39; 模块&#39;,&#39; &#39;,&#39; 非零&#39;,&#39; &#39;,&#39; 减少&#39;,&#39; reduce_ex &#39;,&#39; repr &#39;,&#39; setattr &#39;,&#39; < strong> setstate &#39;,&#39; sizeof &#39;,&#39; str &#39;,&#39; 子类别&#39;,&#39; 弱点&#39;,&#39; _add_hints&#39;, &#39; _as_sql&#39;,&#39; _base_queryset_class&#39;,&#39; _batched_insert&#39;,&#39; _clone&#39;,&#39; _create_object_from_params&#39;,&#39; _db& #39;,&#39; _earliest_or_latest&#39;,&#39; _extract_model_params&#39;,&#39; _fetch_all&#39;,&#39; _fields&#39;,&#39; _filter_or_exclude&#39;,& #39; _for_write&#39;,&#39; _has_filters&#39;,&#39; _hints&#39;,&#39; _insert&#39;,&#39; _known_related_objects&#39;,&#39; _merge_known_related_objects&# 39;,&#39; _merge_sanity_check&#39;,&#39; _next_is_sticky&#39;,&#39; _populate_pk_values&#39;,&#39; _prefetch_done&#39;,&#39; _prefetch_related_lookups&#39;,&# 39; _prefetch_related_objects&#39;,&#39; _prepare&#39;,&#39; _raw_delete&#39;,&#39; _result_cache&#39;,&#39; _setup_aggregate_query&#39;,&#39; _setup_query&#39 ;,&#39; _specialized_queryset_class&#39;,&#39; _sticky_filter&#39;,&#39; _update&#39;,&#39;聚合&#39;,&#39;所有&#39;,&#39 ;注释&#39;,&#39; annotation_names&#39; as_manager&#39;,&#39; bulk_create&#39;,&#39; complex_filter&#39;,&#39;计算&#39;,&#39;创建&#39;,&#39;日期&#39;,&#39; datetimes&#39;,&#39; db&#39;,&#39; defer&#39;, &#39;删除&#39;,&#39; distinct&#39;,&#39;最早&#39;,&#39;排除&#39;,&#39;存在&#39;,&#39;额外& #39;,&#39; extra_names&#39;,&#39; field_names&#39;,&#39; filter&#39;,&#39; first&#39;,&#39; get&#39;,& #39; get_or_create&#39;,&#39; in_bulk&#39;,&#39; is_compatible_query_object_type&#39;,&#39; iterator&#39;,&#39; last&#39;,&#39; latest&# 39;,&#39; model&#39;,&#39; none&#39;,&#39; only&#39;,&#39; order_by&#39;,&#39; ordered&#39;,&# 39; prefetch_related&#39;,&#39;查询&#39;,&#39; raw&#39;,&#39;反向&#39;,&#39; select_for_update&#39;,&#39; select_related&#39 ;,&#39;更新&#39;,&#39; update_or_create&#39;,&#39;使用&#39;,&#39; value_annotation&#39;,&#39;值&#39;,&#39; ; values_list&#39;]

这是python数组的目录:

dir([])

[&#39; 添加&#39;,&#39; &#39;,&#39; 包含 &#39;,&#39; delattr &#39;,&#39; delitem &#39;,&#39; delslice &#39;,&#39; doc &#39;,&#39; eq &#39;,&#39; 格式&#39;,&#39; ge &#39;,&#39; getattribute &#39;,&#39; getitem < / strong>&#39;,&#39; getslice &#39;,&#39; gt &#39;,&#39; 哈希&#39;,&#39; iadd &#39;,&#39; imul &#39;,&#39; init &#39;,&#39; iter &#39;,&#39; le &#39;,&#39; len &#39;,&#39; lt &#39;,&#39; mul &#39;,&#39; < strong> ne &#39;,&#39; &#39;,&#39; reduce &#39;,&#39; reduce_ex &#39;,&#39; repr &#39;,&#39; 颠倒&#39;,&#39 ;的 rmul &#39;,&#39; setattr &#39;,&#39; setitem &#39;,&#39; setslice &#39;,&#39; sizeof &#39;,&#39; str &#39;,&#39; < strong>子类分类&#39;,&#39;追加&#39;,&#39;计数&#39;,&#39;延伸&#39;,&#39;索引&#39;,&# 39;插入&#39;,&#39; pop&#39;,&#39;删除&#39;,&#39;反向&#39;,&#39;排序&#39;]

答案 1 :(得分:1)

Devices.objects.filter(device_id = field.device_id).values()是ValuesQuerySet,它派生自QuerySet,两者都是unevaluated,这意味着这是一个查询在应用列表或迭代之前,不会在数据库上执行。 当您使用json.dumps时,您需要不查询数据。

以下申请查看类型:

Devices.objects.filter(device_id=field.device_id).values().__class__
  

django.db.models.query.ValuesQuerySet

Devices.objects.filter(device_id=field.device_id).values().__class__.__base__
  

django.db.models.query.QuerySet