Django QuerySet发行

Django QuerySet发行,django,orm,Django,Orm,我有一个模型,其列team\u id可以为空。我想检查它是null还是None team_id = Profile.objects.values_list('team_id').filter(user_id=request.user) print(team_id) if not team_id: print("none") else: print("not none") 我得到以下输出: <QuerySet [(None,

我有一个模型,其列
team\u id
可以为空。我想检查它是
null
还是
None

    team_id = Profile.objects.values_list('team_id').filter(user_id=request.user)
    print(team_id)

    if not team_id:
        print("none")
    else:
        print("not none")
我得到以下输出:

<QuerySet [(None,)]>
not none

但是给了我同样的结果…有什么想法吗?

因为你有一个包含一个元组的列表,其中包含一个值None。列表本身不等于None。

因为列表包含一个元组,元组包含一个值None。列表本身并不等于“无”。

假设每个用户只有一条记录,从您的描述/代码中可以看出:

try:
    profile = Profile.objects.get(user_id=request.user)
    if profile.team_id:
        print 'not none'
    else:
        print 'none'
except Profile.DoesNotExist:
    print 'profile not found'

假设每个用户只有一条记录,从您的描述/代码中可以看出:

try:
    profile = Profile.objects.get(user_id=request.user)
    if profile.team_id:
        print 'not none'
    else:
        print 'none'
except Profile.DoesNotExist:
    print 'profile not found'