[Fixed]-Formulating an efficient DB query with highly challenging requirements (Django app)

1👍

Get the banned users list like:

banlist = HellBanList.objects.values_list('condemned',flat=True)

modify freshest_reply query like:

freshest_reply = Publicreply.objects.filter(answer_to=freshest_link).exclude(submitted_by_id__in=banlist).latest('submitted_on')

2nd method:

In your query, first you get the Link which has the latest public reply and in the next query you get the latest Publicreply for that Link. Instead of that we can just get the latest reply in one query like:

latest_reply = Publicreply.objects.filter(Q(answer_to__submitter=self.request.user)|Q(submitted_by=self.request.user)).exclude(submitted_by_id__in=banlist).latest('submitted_on')

Let me know if I missed something.

Leave a comment