Answer the question
In order to leave comments, you need to log in
Do I need to filter out unnecessary requests in the code?
Hello!
Here is a small piece of code for displaying messages between users:
messages = QuerySet #тут queryset с сообщениями
context['message_list'] = messages
# мне нужно пометить просмотренными сообщения для получателя
# обычно я постоянно делаю запрос на обновление:
Message.objects.filter(recipient=self.request.user, viewed__isnull=True).update(viewed=timezone.now())
# но ведь можно заранее проверить, есть ли новые сообщения и если нужно сделать запрос (вариант 2):
not_viewed_message_ids = [msg.pk for msg in messages if msg.recipient == self.request.user and not viewed]
if not_viewed_message_ids:
Message.objects.filter(pk__in=not_viewed_message_ids).update(viewed=timezone.now())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question