Reputation: 145
i'm trying to sort some model-data inside a class based view by the currently logged in user. How do i do that? I tried the following, but it didnt work:
class ChatOverView(ListView):
def get_queryset(self):
return [Message.objects.filter(sender=self.request.user),
Message.objects.filter(receiver=self.request.user)]
model = {"received": get_queryset()[0], "sent": get_queryset()[1]}
template_name = "chat/home-chat.html"
Im getting the following error message: TypeError: get_queryset() missing 1 required positional argument: 'self'
Thanks for Your Help!
Edit I am aiming to implement a chat-system on my website. In order to do that, i set up a database which stores all sent messages. Each entry contains a "sender" and a "receiver" field. With Help of this View i want to display all messages the currently logged in user received or sent.
Upvotes: 1
Views: 29
Reputation: 477794
You can simply override the get_context_data
. You can let the Django ListView
handle one of the lists, and handle the other one yourself:
class ChatOverView(ListView):
model = Message
context_object_name = 'sent'
template_name = 'chat/home-chat.html'
def get_queryset(self):
return super().get_queryset().filter(
sender=self.request.user
)
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context.update(
received=Message.objects.filter(receiver=self.request.user)
)
return context
Upvotes: 1