0

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

1 Answer 1

1

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'))})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.