【发布时间】:2020-01-27 06:29:14
【问题描述】:
这是我尝试过的代码
staffids = [1,2,4,5,6,7]
# up_obj = UserProfile.objects.filter(userId__in = staffids) #i will explain it below
flag=0
unknown = []
for x in staffids:
up_obj = UserProfile.objects.filter(userId=x)
if up_obj.exists():
print(up_obj)
else:
flag=1
unknown.append({
"id": x
})
if flag == 1:
return Response({
STATUS:False,
MESSAGE:"User not found",
DATA:unknown
})
此代码仅返回可用用户
up_obj = UserProfile.objects.filter(userId__in = staffids)
假设如果用户标识 3,4 不可用,则此代码返回没有 3,4 的查询集。
如果所有 id 都不存在,我想返回一条消息,指出以下 id 不可用或其他...,我已经尝试过,但我正在寻找更好的方法来做到这一点
django 有内置方法吗?
【问题讨论】:
-
您能否更简洁地说明您在寻找什么?
-
好的,假设我有一个用户 ID 列表,比如 1,2,3,4,5 。我想要的是表中是否所有 id 都可用,如果表中没有某些 id,我想找回这些 id,我必须做更多的事情
-
@Jasir 在下面看到我的回答,你得到的正是你所描述的。
标签: django django-models orm django-rest-framework