作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是 Django1.9 的新手, 模型.py:- class MyUser(models.Model): UserId = models.UUIDField(primary_key=True
OrderedDict([('red', 10.0),('blue',45.5),('green',34.4)] xList[(u'blue',1.5,u'car'),(u'green',1.9,u'
我是一名优秀的程序员,十分优秀!