检查Django中的空查询 [英] Checking for empty queryset in Django

查看:148
本文介绍了检查Django中的空查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询查询是否返回任何结果的建议成语是什么?

示例:

What is the recommended idiom for checking whether a query returned any results?
Example:

orgs = Organisation.objects.filter(name__iexact = 'Fjuk inc')
# If any results
    # Do this with the results without querying again.
# Else, do something else...

我想有几种不同的方式检查这个,但我想知道一个有经验的Django用户将如何做。
文档中的大多数示例只是忽略没有发现的情况...

I suppose there are several different ways of checking this, but I'd like to know how an experienced Django user would do it. Most examples in the docs just ignore the case where nothing was found...

推荐答案

if not orgs:
    # Do this...
else:
    # Do that...

这篇关于检查Django中的空查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆