gpt4 book ai didi

django - DRF Serializer - OrderDict 而不是 JSON

转载 作者:行者123 更新时间:2023-12-05 08:23:24 25 4
gpt4 key购买 nike

我是 Django1.9 的新手,

模型.py:-

class MyUser(models.Model):

UserId = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
UserFirstName = models.CharField(max_length=1000)
UserLastName = models.CharField(max_length=1000)
UserEmail = models.EmailField(max_length=1000,blank=False,null=True)
UserContactNumber = models.CharField(max_length=1000)
UserPassword = models.CharField(max_length=1000)
IsActive = models.BooleanField(default=False)
Role = ArrayField(models.CharField(max_length=1000), blank=True,null=True)

class Meta:
ordering =('IsActive',)

序列化程序.py:-

class MyUserSerializer(serializers.ModelSerializer):

class Meta:
model = MyUser
fields = '__all__'

外壳输出:-

>>> from projectmanagement.serializers import MyUserSerializer
>>> users = MyUser.objects.all()
>>> users
[<MyUser: MyUser object>, <MyUser: MyUser object>]
>>> serializer = MyUserSerializer(users,many=True)
>>> serializer
MyUserSerializer([<MyUser: MyUser object>, <MyUser: MyUser object>], many=True):
UserId = UUIDField(label='UserId', read_only=True)
UserFirstName = CharField(label='UserFirstName', max_length=1000)
UserLastName = CharField(label='UserLastName', max_length=1000)
UserEmail = EmailField(allow_null=True, label='UserEmail', max_length=1000, required=False)
UserContactNumber = CharField(label='UserContactNumber', max_length=1000)
UserPassword = CharField(label='UserPassword', max_length=1000)
IsActive = BooleanField(label='IsActive', required=False)
Role = ListField(allow_null=True, child=CharField(label='Role', max_length=1000), required=False)
>>> serializer.data
[OrderedDict([('UserId', 'fd5df8d3-a578-4fe7-95ea-172ad2399ff8'), ('UserFirstName', u''), ('UserLastName', u''), ('UserEmail', None), ('UserContactNumber', u''), ('UserPassword', u''), ('IsActive', False), ('Role', None)]), OrderedDict([('UserId', '8301e1b6-a031-443f-957a-df98025e5e9f'), ('UserFirstName', u'Piyush'), ('UserLastName', u'Wanare'), ('UserEmail', u'piyush@uniserved.com'), ('UserContactNumber', u'992053268236'), ('UserPassword', u'Piyush@1234'), ('IsActive', True), ('Role', [u'Vendor Cordinator'])])]

为什么我没有得到 JSON 数据而不是 OrderedDict?如何获取 JSON 格式的数据?

最佳答案

我猜您正在使用 Django REST Framework?

DRF 的教程API 指南 中没有详细记录。但实际上在 Tutorial 1: Serialization 中给出了用于序列化查询集的示例:

serializer = SnippetSerializer(Snippet.objects.all(), many=True)
serializer.data
# [OrderedDict([('id', 1), ('title', u''), ('code', u'foo = "bar"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 2), ('title', u''), ('code', u'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 3), ('title', u''), ('code', u'print "hello, world"'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])]

获取JSON格式的数据:

import json
serializer = MyUserSerializer(users,many=True)
json.dumps(serializer.data)

关于django - DRF Serializer - OrderDict 而不是 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44021903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com