QuerySet类型的对象不是JSON可序列化的Django JSON



在我尝试返回JsonResponse
之前,一切都很正常我正在循环浏览包含用户名的列表,然后将这些用户名传递给返回基本用户信息的模型,并将这些值存储在一个变量中,我后来将该变量添加到列表中。

def jsonresponse(request, username):
Current_User = User.objects.get(username=username)
#List of who this user follows
followingList = Follower.objects.filter(follower=int(Current_User.id))
UsersInfo = []
for user in followingList:
singleUser = User.objects.filter(username=user.following).values( 'username','bio', 'profile_image')
UsersInfo.append(singleUser)

results = User.objects.filter(username=Current_User).values( 'username','bio', 'profile_image') **This Works**
return JsonResponse({'results':list(results), 'UsersInfo':list(UsersInfo)})

这项工作"结果":列表(结果(,这不是'UsersInfo':列表(UsersInfo(

打印(结果(显示:

<QuerySet [{'username': 'John', 'bio': 'Hello, im new!', 'profile_image': 'images/ape_xhRtC2R.jpg'}]>

print(UsersInfo(提供以下信息:

[<QuerySet [{'username': 'Tristan', 'bio': 'Hello, im new!', 'profile_image': 'images/1-scary-tiger-head-robbybubble.jpg'}]>, <QuerySet [{'username': 'William', 'bio': 'Hello, im new!', 'profile_image': 'images/ape_PvPNwCP.jpg'}]>]

任何帮助都会通知

更换

singleUser = User.objects.filter(username=user.following).values( 'username','bio', 'profile_image')

singleUser = User.objects.get(username=user.following).values( 'username','bio', 'profile_image')

您每次只需获取QuerySet中的用户对象。

最新更新