python 一个对象的 Django 序列化程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2391002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:32:47  来源:igfitidea点击:

Django serializer for one object

pythondjangojson

提问by khelll

I'm trying to figure out a way to serialize some Django model object to JSON format, something like:

我试图找出一种将某些 Django 模型对象序列化为 JSON 格式的方法,例如:

j = Job.objects.get(pk=1)
##############################################
#a way to get the JSON for that j variable???
##############################################

I don't want:

我不想:

from django.core import serializers
serializers.serialize('json', Job.objects.get(pk=1),ensure_ascii=False)

Because it returns JSON array, not a single object representation.

因为它返回 JSON 数组,而不是单个对象表示。

Any ideas?

有任何想法吗?

One way I'm thinking of: is to find a way to get a hash(attribute,value) of the object and then use simplejson to get the JSON representation of it, however I don't know how to get that hash.

我想到的一种方法是:找到一种方法来获取对象的哈希(属性,值),然后使用 simplejson 获取它的 JSON 表示,但是我不知道如何获取该哈希。

回答by istruble

How about just massaging what you get back from serializers.serialize? It is not that hard to trim off the square brackets from the front and back of the result.

只是按摩你从 serializers.serialize 得到的东西怎么样?从结果的正面和背面修剪掉方括号并不难。

job = Job.objects.get(pk=1)
array_result = serializers.serialize('json', [job], ensure_ascii=False)
just_object_result = array_result[1:-1]

Not a fancy answer but it will give you just the object in json notation.

不是一个花哨的答案,但它只会给你 json 符号中的对象。

回答by Pavel Daynyak

I would suggest using Django's model_to_dict. If I'm not mistaken, serializers.serialize()relies on it, too, but it only works for list, not single model instance. That's how you get a dictinstance with your model fields out of a single model:

我建议使用 Django 的model_to_dict. 如果我没记错的serializers.serialize()话,也依赖它,但它只适用于list而不是单个模型实例。这就是您如何dict从单个模型中获取包含模型字段的实例的方法:

from django.forms.models import model_to_dict

# assuming obj is your model instance
dict_obj = model_to_dict( obj )

You now just need one straight json.dumpscall:

你现在只需要一个直接的json.dumps电话:

import json
json.dumps(dict_obj)

回答by JPG

Method-1

方法一

Use Django Serializer with pythonformat

使用带python格式的Django Serializer

from django.core import serializers

j = Job.objects.get(pk=1)
response = serializers.serialize('python', [j], ensure_ascii=False)


Method-2

方法二

use jsonformat while serializing and loadsthe string response

json在序列化和加载字符串响应时使用格式

import json
from django.core import serializers

j = Job.objects.get(pk=1)
json_str_response = serializers.serialize('json', [j], ensure_ascii=False)
response = json.loads(json_str_response)[0]


Method-3

方法三

Use Django REST Framework's Serializer classdefine a serializer class and serialize the instance as

使用Django REST Framework 的 Serializer 类定义一个序列化器类并将实例序列化为

from rest_framework import serializers


class JobSerializer(serializers.ModelSerializer):
    class Meta:
        model = Job
        fields = '__all__'


j = Job.objects.get(pk=1)
response = JobSerializer(instance=j).data


Reference
1. Serializer Django model object

参考
1. Serializer Django 模型对象