cryg0
cryg0

Reputation: 55

Issue with django json response

I have issue with json response. messages.values() gives me user_id instead of username. How to get username?

Here is my code:

class LivechatMessage(models.Model):
    user=models.ForeignKey(User,on_delete=models.CASCADE, null=True)


def getMessages(request):
    messages =LivechatMessage.objects.all()
    return JsonResponse({"messages":list(messages.values())})

Upvotes: 0

Views: 405

Answers (1)

Ankit Tiwari
Ankit Tiwari

Reputation: 4690

You can do like this to access related attributes

def getMessages(request):
    messages =LivechatMessage.objects.all()
    return JsonResponse({"messages":list(messages.values('user__username','user__id'))})

Upvotes: 1

Related Questions