1π
β
I would use user_passes_test()
here since you specifically want to restrict specific views.
First, define a couple of functions that return True
when youβre dealing with a user who should be able to see your content. It looks like your UserTypeOne
and UserTypeTwo
models extend the base User
model with a one-to-one relationship, so you can use hasattr
to check if a given base user has one of those attributes:
def type_one_only(user):
if hasattr (user, 'usertypeone'):
return True
else:
return False
def type_two_only(user):
#same thing without if/else
return hasattr(user, 'usertypetwo')
Now when you have a view that you want to restrict to one user type, you can add a user_passes_test
decorator before it:
@user_passes_test(type_one_only, login_url='/')
def my_view(request):
...
login_url
is where a user will be sent if they do not pass the test youβve indicated.
π€souldeux
Source:stackexchange.com