有很多关于如何序列化模型QuerySet的文档,但是如何将模型实例的字段序列化为JSON呢?


当前回答

为了避免数组包装器,在返回响应之前删除它:

import json
from django.core import serializers

def getObject(request, id):
    obj = MyModel.objects.get(pk=id)
    data = serializers.serialize('json', [obj,])
    struct = json.loads(data)
    data = json.dumps(struct[0])
    return HttpResponse(data, mimetype='application/json')

关于这个话题,我也发现了一篇有趣的帖子:

http://timsaylor.com/convert-django-model-instances-to-dictionaries

它使用django.forms.models。Model_to_dict,它看起来是完成这项工作的完美工具。

其他回答

下面是我的解决方案,它允许您轻松地自定义JSON以及组织相关记录

首先在模型上实现一个方法。我调用的是json,但你可以叫它任何你喜欢的,例如:

class Car(Model):
    ...
    def json(self):
        return {
            'manufacturer': self.manufacturer.name,
            'model': self.model,
            'colors': [color.json for color in self.colors.all()],
        }

然后在视图中:

data = [car.json for car in Car.objects.all()]
return HttpResponse(json.dumps(data), content_type='application/json; charset=UTF-8', status=status)

似乎你不能序列化实例,你必须序列化一个对象的QuerySet。

from django.core import serializers
from models import *

def getUser(request):
    return HttpResponse(json(Users.objects.filter(id=88)))

我运行了django的svn释放,所以这可能不是在早期版本。

如果你处理的是一个模型实例列表,你能做的最好的就是使用serializer .serialize(),它会非常适合你的需要。

但是,您将面临试图序列化单个对象而不是对象列表的问题。这样,为了摆脱不同的黑客,只需使用Django的model_to_dict(如果我没有弄错的话,serializer .serialize()也依赖于它):

from django.forms.models import model_to_dict

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

现在只需要一个直接的json。Dumps调用,将其序列化为json:

import json
serialized = json.dumps(dict_obj)

就是这样!:)

如果你正在询问如何序列化一个模型中的单个对象,并且你知道你只会在queryset中获得一个对象(例如,使用objects.get),那么可以使用如下方法:

import django.core.serializers
import django.http
import models

def jsonExample(request,poll_id):
    s = django.core.serializers.serialize('json',[models.Poll.objects.get(id=poll_id)])
    # s is a string with [] around it, so strip them off
    o=s.strip("[]")
    return django.http.HttpResponse(o, mimetype="application/json")

你会得到这样的东西:

{"pk": 1, "model": "polls.poll", "fields": {"pub_date": "2013-06-27T02:29:38.284Z", "question": "What's up?"}}

It sounds like what you're asking about involves serializing the data structure of a Django model instance for interoperability. The other posters are correct: if you wanted the serialized form to be used with a python application that can query the database via Django's api, then you would wan to serialize a queryset with one object. If, on the other hand, what you need is a way to re-inflate the model instance somewhere else without touching the database or without using Django, then you have a little bit of work to do.

我是这么做的:

首先,我使用demjson进行转换。它碰巧是我最先发现的,但它可能不是最好的。我的实现取决于它的一个特性,但其他转换器应该也有类似的方法。

其次,在所有可能需要序列化的模型上实现json_equivalent方法。对于demjson来说,这是一个神奇的方法,但无论您选择什么实现,都可能需要考虑这一点。这个想法是你返回一个对象,可以直接转换为json(即一个数组或字典)。如果你真的想自动执行:

def json_equivalent(self):
    dictionary = {}
    for field in self._meta.get_all_field_names()
        dictionary[field] = self.__getattribute__(field)
    return dictionary

这对你没有帮助,除非你有一个完全扁平的数据结构(没有foreignkey,数据库中只有数字和字符串等)。否则,您应该认真考虑实现此方法的正确方法。

第三,调用demjson.JSON.encode(instance),你就得到了你想要的东西。