Django - user permissions to certain views?

后端 未结 5 909
夕颜
夕颜 2020-12-14 01:45

From the admin I see that you can allocate permissions to a user or a user group to :allow add, change or delete data from a model.

That is great, but I also need t

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 02:22

    Permissions system is model-centric and assumes that permissions are tied to models. I think following 2 alternatives are best options:

    A. If your views are related to some specific model, use custom permissions on that model as Marcus Whybrow suggested.

    B. [not tested, might not work] Subclasss User and define your own permissions there. You don't need actual model, it's just wrapper for your app's custom permission:

    from django.contrib.auth.models import User
    class MyUser(User):
        class Meta:
            permissions = (('can_visit_$viewset1', 'Can visit $view_set_1'))
    

    Don't forget to run syncdb to add custom permissions to database.

提交回复
热议问题