简体   繁体   中英

Object of type QuerySet is not JSON serializable Django JSON

Everything works until I try to Return JsonResponse
I'm looping through the list, which contains usernames then passing those usernames to the models which return basic user information and storing those values in a variable which I later added to the list.

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)})

This works 'results':list(results) , This doesn't 'UsersInfo':list(UsersInfo)

print(results) gives me this:
<QuerySet [{'username': 'John', 'bio': 'Hello, im new,': 'profile_image'. 'images/ape_xhRtC2R.jpg'}]>

print(UsersInfo) gives me this:
[<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'}]>]

Any help would really be appriciated

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM