python - 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)
我想排除exists
我otherBlogs
该怎么做?
提前致谢...
解决方案
我想你想要...
Blog.objects.exclude(blog=exists)
...因为exclude()
采用过滤器样式的表达式。
推荐阅读
- sql - 需要可行的解决方案来解决我的无效条件创建
- node.js - 属性“chatServices”没有初始值设定项,也没有在构造函数中明确分配
- dataframe - 使用 Python 从数据框列中的列表中获取相同的值
- mysql - 更新具有 5 列的表中的 1 行,如果未更改,则使其他列具有旧值
- javascript - jQuery QueryBuilder:如何初始化它并设置只读参数
- python - 多语言错误:输入在字节 333789(361147)周围包含无效的 UTF-8
- python - 如何查询 csv 文件并更新值
- sql - 是否可以在 SQL 中索引 JSON 数组中的属性?
- reactjs - 在所有页面中反应路由器 404 页面渲染
- python - 确定该月给定日期的星期几。给定每月第一天的工作日