我正在编写收件箱视图,并且希望从看到的消息中排除看不见的消息。 我的代码是:排除查询集中的查询集的最佳方式
高清render_notifications(请求):
unseen_notices = notification.Notice.objects.filter(recipient=request.user,unseen=True).order_by("-added") notices = notification.Notice.objects.filter(recipient=request.user).exclude(id__in = [i.id for i in unseen_notices]).order_by("-added") for msg in unseen_notices: msg.unseen = False msg.save() context = RequestContext(request,{'notices':notices, 'unseen_notices':unseen_notices}) return render_to_response('my_notifications.html',context_instance=context)
正如你可以看到我排除使用for循环这似乎并不方便所有看不见的消息,我不当然,但这条线会导致性能问题?最后一点是没有办法,我可以替换类似的东西这条线的方式:
通知= notification.Notice.objects.filter(收件人= request.user).exclude(id__in = unseen_messages)
编辑:
我不能得到看到消息正确使用看不见=假,因为我标记所有看不见的消息被访问页面时所看到。出现奇怪的原因,当我在查询通知中使用unseen = False时,从通知查询中检索到所有未标记的消息。
只是好奇,你可以不过滤'unseen = False'?即'notices = notification.Notice.objects.filter(receivingient = request.user,unseen = False)...' –
如果我这样做了,那些看不见的消息将被标记为可见,并且不知道为什么,但它们在通知查询集。 – iva123