gpt4 book ai didi

django - 对象没有属性 'exists'

转载 作者:行者123 更新时间:2023-12-01 08:15:42 26 4
gpt4 key购买 nike

我正在使用 Django 1.3 并尝试在模型条目上使用 .exists() 但得到下面列出的错误。 Exists() 包含在 Django 1.2 中,所以我应该可以访问它。我使用 django.get_version 验证了我的版本,没问题。
基于 pk 查询 MyModel 仅返回一个条目,但使用 .exists() 查询会引发错误。我需要进口东西吗?

>>> m = MyModel.objects.get(pk=1)
>>> m
<MyModel: Model field entry 1>
>>> m = MyModel.objects.get(pk=1).exists()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'MyModel' object has no attribute 'exists'

最佳答案

exists() is a method of a QuerySet .
get()返回单个模型实例,和 will raise an exception Entry.DoesNotExist if that instance doesn't exist .所以你需要把它包装在 try/except 中如果您不确定实例是否具有该 id,请阻止存在。

关于django - 对象没有属性 'exists',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11093087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com