简体   繁体   English

exist()的确切用法是什么?

[英]What is the exact usage of exists()?

I am using it as if ModelName.objects.get(fieldname=value).exists(): 我正在使用它, if ModelName.objects.get(fieldname=value).exists():

I either get the error 'ModelName' object has no attribute 'exists' or it throws a DoesNotExist exception. 我或者收到错误的“ ModelName”对象没有属性“ exists”,或者抛出DoesNotExist异常。

Related doc: https://docs.djangoproject.com/en/1.10/ref/models/querysets/#django.db.models.query.QuerySet.exists 相关文档: https : //docs.djangoproject.com/en/1.10/ref/models/querysets/#django.db.models.query.QuerySet.exists

You should apply it to queryset, not a single object, try: 您应该将其应用于queryset而不是单个对象,请尝试:

if ModelName.objects.filter(fieldname=value).exists():

If queryset is not empty, this will return True . 如果queryset不为空,则将返回True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM