0

I have no idea what this means:

(success, admin_id) = login(email, password)

The login method looks something like this:

def login(email, password):
    try:
        user = db.session.query(Admin).filter_by(email=email).first()        

        valid_role = 'Admin'

        if valid_role in user.role:       
            return user
    except Exception as e:
        print e

    return None

It returns either a TypeError: 'Admin' object is not iterable or a ValueError: too many values to unpack when I try to return just the user.id. Thank you for any help.

0

2 Answers 2

2

To return multiple values (tuple) from your function you can do:

# when user is admin
return True, user.id

In the case the user is not an admin or there is an exception:

return False, None

So with (success, admin_id) = login(email, password), the success status is returned from your function as a boolean, and the admin_id is either a valid user id or None

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this is exactly what I'm looking for.
So I am assigning two variables. What is the difference between having the parenthesis and not having the parenthesis?
The parenthesis serves for readability. You can as well do without them
2

Your function returns one object; either an Admin() instance:

if valid_role in user.role:       
    return user

or None at the end:

return None

Neither fits the expectation that the function returns two values, which is what this line expects:

(success, admin_id) = login(email, password)

Either adjust that line to only receive the user, or fix your function to return the user id and the success flag.

The latter would require that you rewrite your function to:

def login(email, password):
    try:
        user = db.session.query(Admin).filter_by(email=email).first()        

        valid_role = 'Admin'

        if valid_role in user.role:       
            return True, user.id
    except Exception as e:
        print e

    return False, None

Now your function always returns two items, either True and the user id (not the user object), or False and None.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.