Check if an object exists

前端 未结 6 1919
孤独总比滥情好
孤独总比滥情好 2021-02-02 04:57

I need to check if Model.objects.filter(...) turned up anything, but do not need to insert anything. My code so far is:

user_pass = log_in(request.P         


        
6条回答
  •  庸人自扰
    2021-02-02 05:49

    If the user exists you can get the user in user_object else user_object will be None.

    try:
        user_object = User.objects.get(email = cleaned_info['username'])
    except User.DoesNotExist:
        user_object = None
    if user_object:
        # user exist
        pass
    else:
        # user does not exist
        pass
    

提交回复
热议问题