我正在使用rest_framework.response方法发送多个
django模型的对象.但是,响应包含反斜杠的引号.这是我的观点:
@api_view()
def myfunctions(request,id):
responseData = {}
userObject = TifUser.objects.filter(id=id)
attendances = Attendance.objects.filter(User=userObject)
leaves = Leave.objects.filter(User=userObject)
odds = ODD.objects.filter(User=userObject)
printjobs = PrintJob.objects.filter(User=userObject)
issues = Issue.objects.filter(User=userObject)
#serialize into json
userObject = serializers.serialize("json", userObject)
attendances = serializers.serialize("json",attendances)
leaves = serializers.serialize("json",leaves)
odds = serializers.serialize("json",odds)
printjobs = serializers.serialize("json",printjobs)
issues = serializers.serialize("json",issues)
#set responseData dictionary values
responseData['user'] = userObject
responseData['attendances'] = attendances
responseData['leaves'] = leaves
responseData['odds'] = odds
responseData['printjobs'] = printjobs
responseData['issues'] = issues
#responseData['attendances'] = userObject
return response.Response(responseData)
我得到的json响应是:
{"attendances":"[{\"model\": \"mainApp.attendance\", \"pk\": 5, \"fields\": {\"ArrivalTime\": \"2016-06-27T18:45:46.355Z\", \"DepartureTime\": null, \"User\": 4, \"ArrivalImei\": \"1\", \"DepartureImei\": null, \"Hash\": \"321f059c-4230-417a-adff-f0035097c85d\"}}, {\"model\": \"mainApp.attendance\", \"pk\": 13, \"fields\": {\"ArrivalTime\": \"2016-07-18T15:40:39.943Z\", \"DepartureTime\": null, \"User\": 4, \"ArrivalImei\": \"2\", \"DepartureImei\": null, \"Hash\": \"e61fad3e-8238-46fc-b09b-8b7754d43f3b\"}}]","printjobs":"[{\"model\": \"mainApp.printjob\", \"pk\": 1, \"fields\": {\"User\": 4, \"DateAdded\": \"2016-07-18\", \"Status\": \"disapproved\", \"Person\": 5, \"Level\": \"boss\", \"Client\": \"someone\", \"HandledBy\": \"tester\", \"SanctionedBy\": \"myself\", \"AdvancePayment\": 0, \"FinalPayment\": 1000, \"PaymentNumber\": 1, \"Remarks\": \"something\"}}]","odds":"[]","user":"[{\"model\": \"mainApp.tifuser\", \"pk\": 4, \"fields\": {\"AuthUser\": 7, \"Head\": null, \"Boss\": null, \"ClrLevel\": 1, \"Department\": 1, \"DesignationName\": 1, \"Name\": \"tester\", \"IsRegistered\": true, \"DateOfBirth\": \"1222-11-11\", \"Anniversary\": \"2001-12-22\", \"Mobile\": \"2134567890\", \"gcmDevice\": null, \"FatherName\": \"\", \"MotherName\": \"\", \"PersonalEmail\": \"\", \"Gender\": \"Male\", \"CurrentAddress\": \"\", \"PermanentAddress\": \"\", \"PANNumber\": \"\", \"AadharCardNumber\": null, \"BloodGroup\": \"\", \"MaritalStatus\": \"Married\", \"ProfilePhoto\": \"\", \"Imei\": \"\"}}]","leaves":"[]","issues":"[]"}
还有其他办法吗?我的理解是,它将数据编码两次(一次在serializers.serialize中,然后在response.Response中).但我不想那样.我想要嵌套对象.像这样的东西:
{"attendances":[{"model": "mainApp.attendance", "pk": 5, "fields": {"ArrivalTime": "2016-06-27T18:45:46.355Z", "DepartureTime": null, "User": 4, "ArrivalImei": "1",...
谁能告诉我如何实现这一目标?提前致谢.
最佳答案 如您所知,您通过将查询集转换为JSON字符串,将它们添加到dict然后将dict传递给response.Response来对所有内容进行双序列化.
使用Django REST Framework的重点是为您处理序列化,因此目标应该是避免在api视图中自行序列化为JSON字符串.
要充分利用DRF,您需要为要在响应中返回的每个模型定义ModelSerializer.
DRF中令人困惑的一件事是术语.通常,当我们谈论“序列化”时,它意味着将对象转换为字符串(即JSON).但是在DRF中,序列化器实际上转换了复杂的对象 – >原始对象.因此,当您使用ModelSerializer“序列化”您的查询集时,它不会生成JSON字符串,而是生成一个原始的python对象(一个字典),然后可以按照传统意义将其序列化为没有错误的JSON字符串.这个“真正的”序列化由Response类完成.
所以我建议作为一个起点:
class TifUserSerializer(serializers.ModelSerializer):
class Meta:
model = TifUser
class AttendanceSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Attendance
class LeaveSerializer(serializers.ModelSerializer):
class Meta:
model = Leave
class ODDSerializer(serializers.ModelSerializer):
class Meta:
model = ODD
class PrintJobSerializer(serializers.ModelSerializer):
class Meta:
model = PrintJob
class IssueSerializer(serializers.ModelSerializer):
class Meta:
model = Issue
@api_view()
def myfunctions(request, id):
users = TifUser.objects.filter(id=id)
user_serializer = TifUserSerializer(users, many=True)
attendances = Attendance.objects.filter(User=userObject)
attendance_serializer = AttendanceSerializer(attendancesv, context={'request': request})
leaves = Leave.objects.filter(User=userObject)
leave_serializer = LeaveSerializer(leaves, many=True)
odds = ODD.objects.filter(User=userObject)
odd_serializer = ODDSerializer(odds, many=True)
printjobs = PrintJob.objects.filter(User=userObject)
printjob_serializer = PrintJobSerializer(printjobs, many=True)
issues = Issue.objects.filter(User=userObject)
issue_serializer = IssueSerializer(issues, many=True)
responseData = {}
responseData['user'] = user_serializer.data
responseData['attendances'] = attendance_serializer.data
responseData['leaves'] = leave_serializer.data
responseData['odds'] = odd_serializer.data
responseData['printjobs'] = printjob_serializer.data
responseData['issues'] = issue_serializer.data
return response.Response(responseData)