luc
luc

Reputation: 43116

In Django, why is user.is_authenticated a method and not a member variable like is_staff

I've lost some time with a bug in my app due to user authentication. I think that it's a bit confusing but maybe someone can explain the reason and it will appear to me very logical.

The user.is_staff is a member variable while user.is_authenticated is a method. However is_authenticated only returns True or False depending if the class is User or AnonymousUser (see http://docs.djangoproject.com/en/dev/topics/auth/)

Is there a reason for that? Why user.is_authenticated is a method?

Thanks in advance

Upvotes: 1

Views: 2311

Answers (3)

is_authenticated() is function which check weather User current object is populated or not.

It is not necessary to store the whether the object is populated or not in database because,

If it is correctly populated then only that particular object data is stored in database.

So, that is_authenticated() is method not a field comming to the user.is_staff is filed which is used to represent the particular user data is comes under staff ex:- super_user . inorder to differentiate user we need to store in data base

Upvotes: 0

Praveen
Praveen

Reputation: 31

is_authenticated() just checks that the user has provided a valid username and password. It would not allow to login user as guest user (If i am not wrong).

Upvotes: 0

gruszczy
gruszczy

Reputation: 42198

Because is_staff is a database field and is_authenticated is not (since that would be strange). Maybe is_authenticated could be turned into a property, but django devs must have decided, that making it a function would be clearer.

Upvotes: 8

Related Questions