Django qry returning related Id not object when getting list(Message.objects.select_related("message_from").filter(message_to=user.user_id).values())

Viewed 26

I'm using Django Reset Framework to return an API response as per the below.

class MesagesTo(APIView):
    def get(self,request):
        try:
            user = User.objects.get(user_id=request.user.user_id)
            Messages = list(Message.objects.select_related("message_from").filter(message_to=user.user_id).values())
            return JsonResponse({"response" : Messages}, safe = False)
        except Exception as ex:
            print(ex)
            return HttpResponseForbidden("Message get failed")

with a model definition of:

class Message(models.Model):
    message_id = models.AutoField(primary_key=True)
    message_from = models.ForeignKey(KSWUser, on_delete=models.DO_NOTHING, null=True, related_name="message_from")
    message_to = models.ForeignKey(KSWUser, on_delete=models.DO_NOTHING, null=True, related_name="message_to")
    message_date = models.DateTimeField(auto_now_add=True, blank=True)
    message_readdate = models.DateTimeField(blank=True, null=True)
    message_title = models.CharField(max_length=255)
    message_body = models.TextField()

    class Meta:
        app_label="quickstart"
        db_table = 'messages'

But the problem is the the response only includes the id of the related object (unsurprising as its the structure of the DB entity) I really want the name and email address members of the user objects.

What query should I make?

Thank you

0 Answers
Related