django - <User: root> is not JSON serializable -


i trying display chart shows number of books related each user , , getting right result in shell, when render template, following error : not json serializable

my template_tag

@register.simple_tag() def chart_data():     users = user.objects.annotate(num_books=count('books'))     return json.dumps({         'labels':[user user in users],         'series':[[user.num_books user in users]]     }) 

you're seeing because you're trying encode python class object json, json module doesn't support serializing python class.

only objects json supports listed here.

to fix this, instead of serializing user object, try serializing user's username. change 5th line this:

'labels':[user.username user in users], 

Comments

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -