首页 > 解决方案 > Django Query - 如何过滤对象以排除单个项目?

问题描述

我正在尝试创建一个博客项目,在该项目中,除了我当前所在的博客之外,我还想在右侧显示其他博客链接?我已经尝试过了,但我得到了一个错误。

这是代码

def redirect(request, slug):
try:
    exists = Blog.objects.get(title=slug)
except Blog.DoesNotExist:
    raise Http404("Page Not Found")
context = {
    'content': exists,
    'otherBlogs': Blog.objects.all().exclude(exists)
}
return render(request, "blog.html", context)

我想排除existsotherBlogs该怎么做?

提前致谢...

标签: pythondjango

解决方案


我想你想要...

Blog.objects.exclude(blog=exists)

...因为exclude()采用过滤器样式的表达式。


推荐阅读