0👍
✅
If I understand correctly, You need a json data which should have a label represents username and series representing book.name for that particular user.
Have you tried :
books = Book.objects.values('user__username', 'name').order_by('user__username', 'name')
1👍
Something like this:
users = User.objects.all()
for user in users:
books = Book.objects.filter(user=user)
0👍
If you want to do it for a particular user, then:
series = Books.objects.filter(user = "username").values("name")
This will produce a queryset:
<queryset: [{name: book1}, {name: book2}, ...]>
You can convert it to list like so:
list(series)
and then send it through json.dumps..
In the above case, you already know the user those books belongs too, but if you want specify it through the query, then:
series = Books.objects.filter(user = "username").values("name", "user__username")
This will produce something like this:
<queryset: [{name: book1, user: username1}, {name: book2, user: username1}, ...]>
Source:stackexchange.com