我需要检查 Model.objects.filter(...)
是否出现任何内容,但不需要插入任何内容。到目前为止我的代码是:
user_pass = log_in(request.POST) # form class
if user_pass.is_valid():
cleaned_info = user_pass.cleaned_data
user_object = User.objects.filter(email = cleaned_info['username'])
最佳答案
我认为从逻辑和效率的角度来看,最简单的方法是使用查询集的 exists() 函数,记录在这里:
所以在你上面的例子中,我会简单地写:
if User.objects.filter(email = cleaned_info['username']).exists():
# at least one object satisfying query exists
else:
# no object satisfying query exists
关于python - 检查对象是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11714536/