从 json 中删除字段

发布于 2024-09-06 01:35:20 字数 1051 浏览 1 评论 0原文

我有一个渲染 json 的视图函数。我能够在 json 中指定我想要的列,但我不知道如何更改关键字段的名称。就像字段“pk”应该是“id”。

我正在使用此自动完成控件(http:// Loopj.com/2009/04/25/jquery-plugin-tokenizing-autocomplete-text-entry/),它要求 json 具有某些字段。

from django.http import HttpResponse
from django.shortcuts import render_to_response
from iCookItThisWay.recipes import models
from django.core import serializers
from django.utils import simplejson

def index(request, template_name):
    meal_types = []
    q = ''

    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']

    if len(q) > 0:
        meal_types = models.MealType.objects.filter(name__istartswith=q)

    json_serializer = serializers.get_serializer("json")()
    sdata = json_serializer.serialize(meal_types, ensure_ascii=False, fields = ('id', 'name'))

    return HttpResponse(simplejson.dumps(sdata), mimetype='application/json')

您还可以给我指出一些文档吗?我觉得我在寻找文档方面很糟糕。

I have a view function which renders json. I am able to specify which columns I want in my json but I don't know how to change the name of the key fields. Like the field "pk" should be "id".

I am using this autocomplete control (http://loopj.com/2009/04/25/jquery-plugin-tokenizing-autocomplete-text-entry/) and it requires the json to have certain fields.

from django.http import HttpResponse
from django.shortcuts import render_to_response
from iCookItThisWay.recipes import models
from django.core import serializers
from django.utils import simplejson

def index(request, template_name):
    meal_types = []
    q = ''

    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']

    if len(q) > 0:
        meal_types = models.MealType.objects.filter(name__istartswith=q)

    json_serializer = serializers.get_serializer("json")()
    sdata = json_serializer.serialize(meal_types, ensure_ascii=False, fields = ('id', 'name'))

    return HttpResponse(simplejson.dumps(sdata), mimetype='application/json')

Could you also please point me to some documentation. I feel that I am crap at finding documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-09-13 01:35:20

您可以手动构建一个 dict 并通过 .dumps() 将其转换为 json,而不是使用序列化器。

meal_types = models.MealType.objects.filter(name__istartswith=q)
results = []
for meal_type in meal_types:
    results.append(
        {'id': meal_type.id,
         'name': meal_type.name})

return HttpResponse(simplejson.dumps(results), mimetype='application/json')

您还可以使用列表理解构建结果,因为只有几个
字段数:

results = [{'id': mt.id, 'name': mt.name} for mt in meal_types]

Instead of using the serializer, you can build a dict manually and convert it to json via .dumps()

meal_types = models.MealType.objects.filter(name__istartswith=q)
results = []
for meal_type in meal_types:
    results.append(
        {'id': meal_type.id,
         'name': meal_type.name})

return HttpResponse(simplejson.dumps(results), mimetype='application/json')

You could also build the results with a list comprehension, since there are only a couple
of fields:

results = [{'id': mt.id, 'name': mt.name} for mt in meal_types]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文