to_friend = User.objects.filter(username=friend_q)[0:1]
If 'friend_q' is NOT inside the User.username...it will give error. What is the recommended tactic?
如果'friend_q'不在User.username中...它将给出错误。推荐的策略是什么?
Thank you
4 个解决方案
#1
9
If friend_q is not a user present in the database, to_friend will be equal to an empty list.
如果friend_q不是数据库中存在的用户,则to_friend将等于空列表。
>>> from django.contrib.auth.models import User
>>> User.objects.filter(username='does-not-exist')
[]
However, it's better to use the get() method to lookup a specific entry:
但是,最好使用get()方法查找特定条目:
>>> User.objects.get(username='does-exist')
<User: does-exist>
>>> User.objects.get(username='does-not-exist')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.5/django/db/models/manager.py", line 120, in get
File "/usr/lib/python2.5/django/db/models/query.py", line 305, in get
DoesNotExist: User matching query does not exist.
You can now catch the DoesNotExist exception and take appropriate actions.
您现在可以捕获DoesNotExist异常并采取适当的操作。
try:
to_friend = User.objects.get(username=friend_q)
except User.DoesNotExist:
# do something, raise error, ...
#2
5
-
FWIW, Username is unique. So U can use
FWIW,用户名是唯一的。所以你可以使用
to_friend = User.objects.get(username=friend_q)
-
If you want to raise a 404 when the user doesn't exist, you may as well,
如果你想在用户不存在时引发404,你也可以,
from django.shortcuts import get_object_or_404 to_friend = get_object_or_404(User,username=friend_q)
-
To prevent error, you may simply put it in the try except block, very pythonic.
为了防止错误,你可以简单地把它放在try除了块之外,非常pythonic。
try: to_friend = User.objects.get(username=friend_q) except User.DoesNotExist: to_friend = None
-
Since this is a common requirement, you should consider defining
get_user_or_none
on the UserManager, and other managers that would requireNone
return, similar to get_object_or_404. This was in consideration for django core, but was not included, for what ever the reason. Still, is a handy, 4 line function, for models that needNone
back.由于这是一个常见的要求,您应该考虑在UserManager上定义get_user_or_none,以及其他需要None返回的管理器,类似于get_object_or_404。这是考虑到django核心,但不包括在内,原因是什么。仍然是一个方便的4线功能,适用于需要无背的模型。
#3
1
The reason you're getting an error is because you're trying to evaluate the Query object when there's nothing in it. The [0:1] at the end is attempting to take the first item out of an empty list. If you split up the expression a little like below, you can check for an empty list before taking an element out of it.
您收到错误的原因是因为您尝试在查询对象中没有任何内容时对其进行评估。最后的[0:1]试图从空列表中取出第一个项目。如果你将表达式拆分得如下,你可以在从中取出一个元素之前检查一个空列表。
to_friends = User.objects.filter(username=friend_q)
if to_friends:
to_friend = to_friends[0]
#4
-1
to_friend = User.objects.filter(username=friend_q).first()
if there is no user with username friend_q, you will get a to_friend = None, if there is, you get the user
如果没有用户名为friend_q的用户,您将获得to_friend = None,如果有,则获得该用户
#1
9
If friend_q is not a user present in the database, to_friend will be equal to an empty list.
如果friend_q不是数据库中存在的用户,则to_friend将等于空列表。
>>> from django.contrib.auth.models import User
>>> User.objects.filter(username='does-not-exist')
[]
However, it's better to use the get() method to lookup a specific entry:
但是,最好使用get()方法查找特定条目:
>>> User.objects.get(username='does-exist')
<User: does-exist>
>>> User.objects.get(username='does-not-exist')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.5/django/db/models/manager.py", line 120, in get
File "/usr/lib/python2.5/django/db/models/query.py", line 305, in get
DoesNotExist: User matching query does not exist.
You can now catch the DoesNotExist exception and take appropriate actions.
您现在可以捕获DoesNotExist异常并采取适当的操作。
try:
to_friend = User.objects.get(username=friend_q)
except User.DoesNotExist:
# do something, raise error, ...
#2
5
-
FWIW, Username is unique. So U can use
FWIW,用户名是唯一的。所以你可以使用
to_friend = User.objects.get(username=friend_q)
-
If you want to raise a 404 when the user doesn't exist, you may as well,
如果你想在用户不存在时引发404,你也可以,
from django.shortcuts import get_object_or_404 to_friend = get_object_or_404(User,username=friend_q)
-
To prevent error, you may simply put it in the try except block, very pythonic.
为了防止错误,你可以简单地把它放在try除了块之外,非常pythonic。
try: to_friend = User.objects.get(username=friend_q) except User.DoesNotExist: to_friend = None
-
Since this is a common requirement, you should consider defining
get_user_or_none
on the UserManager, and other managers that would requireNone
return, similar to get_object_or_404. This was in consideration for django core, but was not included, for what ever the reason. Still, is a handy, 4 line function, for models that needNone
back.由于这是一个常见的要求,您应该考虑在UserManager上定义get_user_or_none,以及其他需要None返回的管理器,类似于get_object_or_404。这是考虑到django核心,但不包括在内,原因是什么。仍然是一个方便的4线功能,适用于需要无背的模型。
#3
1
The reason you're getting an error is because you're trying to evaluate the Query object when there's nothing in it. The [0:1] at the end is attempting to take the first item out of an empty list. If you split up the expression a little like below, you can check for an empty list before taking an element out of it.
您收到错误的原因是因为您尝试在查询对象中没有任何内容时对其进行评估。最后的[0:1]试图从空列表中取出第一个项目。如果你将表达式拆分得如下,你可以在从中取出一个元素之前检查一个空列表。
to_friends = User.objects.filter(username=friend_q)
if to_friends:
to_friend = to_friends[0]
#4
-1
to_friend = User.objects.filter(username=friend_q).first()
if there is no user with username friend_q, you will get a to_friend = None, if there is, you get the user
如果没有用户名为friend_q的用户,您将获得to_friend = None,如果有,则获得该用户