不是基于模型的 Django 身份验证

Not model based Django auth

我正在学习 Django 身份验证,它看起来很棒!
然而,我注意到它似乎有点太基于模型了。

我想到的是假设我有两个网页(监控、统计)并且它们都使用相同的模型(事件table)。我想要的是为不同的用户添加访问监控和统计的权限,例如:can_use_monitoring、can_use_statistics.
我认为将相同的模型添加到这 2 个权限不是问题,但似乎没有必要且具有误导性。还是我遗漏了什么?

您将如何实现?

谢谢,
五、

/////// 更新 /////////
一个想法:我想要的是获得基于网页而不是模型的许可。一个页面可能使用了更多模型,我不知道应该为哪个模型分配权限(因为我只能分配一个)。

I presume it is not a problem to add the same model to these 2 permissions but seems unnecessary and misleading. Or am I missing something?

是的,将相同的模型添加到不同的权限是没有问题的。

通常您应该使用 Django's Groups 功能。创建一个具有一组权限的组,然后继续向该组添加用户。

https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.decorators.user_passes_test

One thought: what I would want is to have permission based on the web pages and not on models. It is possible that one page uses more models and I wouldn't know which model should I assign to the permission (as I can only one).

这可以通过使用 user_passes_testpermission_required 装饰器装饰您的视图来实现。 More here. and here

例如

from django.contrib.auth.decorators import user_passes_test
@user_passes_test(email_check)
def my_view(request):
    ...

def email_check(user):
    return user.email.endswith('@example.com')