gpt4 book ai didi

python - Django Rest Framework - 如何重组 json 响应?

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:07 25 4
gpt4 key购买 nike

我正在使用 Django Rest Framework(连同 rest_framework_mongoengine)并覆盖我的序列化程序的 to_representation() 以发回我自己的自定义 JSON 对象。一切正常,但 JSON 字段的顺序困惑了。请告诉我如何重新排序/重组被发回的 JSON 对象。序列化程序如下所示:

class PharmacySerializer(DocumentSerializer):
bank_account = serializers.DictField(child=serializers.CharField(), required=False)

class Meta:
model = Pharmacy
fields = (
'id', 'name', 'email', 'mobile', 'address_line_1', 'address_line_2',
'city', 'state', 'zip', 'created_by', 'created_on', 'last_updated_by', 'license_number',
'bank_account', 'last_updated_on', 'is_email_verified', 'is_mobile_verified', 'is_active')
read_only_fields = ('created_by', 'created_on', 'last_updated_by', 'last_updated_on', 'is_active')

def to_representation(self, pharmacy):
return {
'id': str(pharmacy.id),
'name': pharmacy.name,
'email': pharmacy.email,
'mobile': pharmacy.mobile,
'address_line_1': pharmacy.address_line_1,
'address_line_2': pharmacy.address_line_2,
'city': pharmacy.city,
'state': pharmacy.state,
'zip': pharmacy.zip,
'created_by': pharmacy.created_by,
'created_on': pharmacy.created_on,
'last_updated_by': pharmacy.last_updated_by,
'license_number': pharmacy.license_number,
'bank_account': {
'bank_name': pharmacy.bank_account.bank_name,
'account_number': pharmacy.bank_account.account_number,
'account_type': pharmacy.bank_account.account_type
},
'last_updated_on': pharmacy.last_updated_on,
'is_email_verified': pharmacy.is_email_verified,
'is_mobile_verified': pharmacy.is_mobile_verified,
'is_active': pharmacy.is_active
}

我得到的回应是:

[
{
"city": "City",
"name": "Some Pharmacy",
"zip": "778372",
"mobile": "9880082229",
"license_number": "SP22",
"is_mobile_verified": false,
"is_active": true,
"created_on": "2016-06-18T19:01:02.901",
"created_by": null,
"email": "somepharmacy@gmail.com",
"state": "State",
"last_updated_by": null,
"is_email_verified": false,
"last_updated_on": "2016-06-18T19:01:02.901",
"address_line_2": null,
"id": "57659a6ea3b6561c0f20265e",
"bank_account": {
"bank_name": "HSBC",
"account_type": "Savings",
"account_number": "663822553"
},
"address_line_1": "Address"
}
]

我希望它与 to_representation() 的返回字典的顺序相同

最佳答案

如果你想保持响应键的顺序,你可以使用 OrderedDict.但是关于 OrderedDicts,您应该记住一件事:

OrderedDict 仅在插入键时保持顺序。使用 kwargs 初始化时订单丢失。

来自 Python docs:

The OrderedDict constructor and update() method both accept keyword arguments, but their order is lost because Python’s function call semantics pass-in keyword arguments using a regular unordered dictionary.

您需要先创建一个OrderedDict实例,然后一个一个地添加键。

from collections import OrderedDict

def to_representation(self, pharmacy):
ret = OrderedDict() # initialize on ordereddict

# insert keys one by one in desired oreder
ret['id'] = str(pharmacy.id)
ret['name'] = pharmacy.name
ret['email'] = pharmacy.email
ret['mobile'] = pharmacy.mobile
ret['address_line_1'] = pharmacy.address_line_1
ret['address_line_2'] = pharmacy.address_line_2
ret['city'] = pharmacy.city
ret['state'] = pharmacy.state
ret['zip'] = pharmacy.zip
ret['created_by'] = pharmacy.created_by
ret['created_on'] = pharmacy.created_on
ret['last_updated_by'] = pharmacy.last_updated_by
ret['license_number'] = pharmacy.license_number
ret['bank_account'] = {
'bank_name' = pharmacy.bank_account.bank_name
'account_number' = pharmacy.bank_account.account_number
'account_type' = pharmacy.bank_account.account_type
}
ret['last_updated_on'] = pharmacy.last_updated_on
ret['is_email_verified'] = pharmacy.is_email_verified
ret['is_mobile_verified'] = pharmacy.is_mobile_verified
ret['is_active'] = pharmacy.is_active

return ret

注意:另一种选择(推荐)是使用EmbeddedDocumentSerializer对于 bank_account 字段。 那么您将不需要覆盖 to_representation() 方法。

关于python - Django Rest Framework - 如何重组 json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37900873/

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