auth.mixins.UserPassesTestMixin.get_test_func()

get_test_func()

You can also override the get_test_func() method to have the mixin use a differently named function for its checks (instead of test_func()).

Stacking UserPassesTestMixin

Due to the way UserPassesTestMixin is implemented, you cannot stack them in your inheritance list. The following does NOT work:

class TestMixin1(UserPassesTestMixin):
    def test_func(self):
        return self.request.user.email.endswith('@example.com')

class TestMixin2(UserPassesTestMixin):
    def test_func(self):
        return self.request.user.username.startswith('django')

class MyView(TestMixin1, TestMixin2, View):
    ...

If TestMixin1 would call super() and take that result into account, TestMixin1 wouldn’t work standalone anymore.

doc_Django
2016-10-09 18:34:15
Comments
Leave a Comment

Please login to continue.