'i need to send in response array of chats in my my chat view from chat model
class MediaUploads(models.Model):
file = models.FileField(upload_to='embla/uploads/')
caption = models.CharField(max_length=256, null=True, blank=True)
owner = models.ForeignKey(User, related_name='embla_uploads',
on_delete=models.CASCADE, null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Profile(models.Model):
...other unlreated keys,
photo = models.ForeignKey(MediaUploads,on_delete=models.DO_NOTHING,related_name='profile_photos',
null=True, blank=True)
name = models.CharField(max_length=256)
...other unrelated keys,
class Message(models.Model):
sender = models.ForeignKey(
to=Profile, on_delete=models.CASCADE, related_name="sender")
receiver = models.ForeignKey(
to=Profile, on_delete=models.CASCADE, related_name="receiver")
message = models.CharField(max_length=999)
timestamp = models.DateTimeField(auto_now_add=True)
is_read = models.BooleanField(default=False)
def __str__(self):
return self.message
class Meta:
ordering = ('timestamp',)
Following is my view:
class ChatView(APIView):
searilzer_class = MessageSearlizer
def get(self, request, *args, **kwargs):
sender_id = self.request.user.profile.pk
# i need this chats here
# On client side i need to show the list of chats along with the names and pictures
# of the people the user is engaged in chat. i have attached the picture of the
# client side view
chats =
return Response({"chats": chats})
I am Nodejs developer and due to insistence of client, doing my first project on django. chat would be array of objects. This array will have 3 keys. receiver name, receiver photo and receiver last message.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

