Python中文
首页
教程
问答
标签
搜索
登录
注册
具有CustomUser的Django权限
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我试图用权限实现基于类的视图,但它们似乎没有连接,尽管我相信我严格遵守了Django用户指南。 第一:在accounts.models中基于代理设置自定义用户模型</p> <pre><code>class CustomUser(AbstractUser): some_fields... </code></pre> <p>然后,我创建了一个经理:</p> <pre><code>class EmployeeManager(models.Manager): def get_queryset(self, *args, **kwargs): return super().get_queryset(*args, **kwargs).filter(status_type=CustomUser.StatusType.EMPLOYEE) </code></pre> <p>然后是配置文件的类型:</p> <pre><code>class Employee(CustomUser): objects = EmployeeManager() class Meta: proxy = True permissions = [("communities.view_region", "Can view region")] </code></pre> <p>在我设置权限的地方,进行迁移和迁移。 之后,我创建视图:</p> <pre><code>import communities.models as comms class RegionsListView(ListView): model = comms.Region </code></pre> <p>然后,配置url及其视图:</p> <pre><code>rom django.urls import path, include import communities.views as views from django.contrib.auth.decorators import permission_required app_name = 'communities' urlpatterns = [ path("regions/list/", permission_required("communities.view_region")(views.RegionsListView.as_view())) </code></pre> <p>然后我以员工身份登录,调用此url时出现错误403</p> <p>我错过了什么? 备注:</p> <ul> <li>在view.py文件中使用<code>permission_required = 'communities.view_region'</code>会产生相同的结果</李> <li>当作为超级用户登录时,我当然会得到正确的页面</李> </ul>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>谢谢你的建议。 然而,仍然有一些事情我没有抓住。。。因此,我寻找了一种新的方法,并开始使用Oso,这是一个很棒的授权库。它工作得很好:<a href="https://www.osohq.com/" rel="nofollow noreferrer">Link to their WebSite</a></p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何添加虚拟方法
10 回答
如何添加表示整数的擦边字符串?
8 回答
如何添加要在Bokeh中使用的新font.ttf文件?
8 回答
如何添加要显示的矩阵XY轴编号和XY轴
8 回答
如何添加计数?
8 回答
如何添加计数器函数?
5 回答
如何添加计数器列来计算数据帧中另一列中的特定值?
1 回答
如何添加计数器来跟踪while循环中的月份和年份?
6 回答
如何添加计数并删除countplot的顶部和右侧脊椎?
1 回答
如何添加计时器wx.应用程序更新窗口对象的主循环?
8 回答
如何添加评论到帖子?PostDetailVew,Django 2.1.5
5 回答
如何添加评论拉梅尔亚姆
3 回答
如何添加诸如矩阵Python/Pandas之类的数据帧?
2 回答
如何添加谷歌地点自动完成到Flask?
8 回答
如何添加超时、python discord bot
1 回答
如何添加超过1dp的检查
3 回答
如何添加距离方法
2 回答
如何添加跟随游戏的敌人精灵
6 回答
如何添加路径以便python可以找到程序?
5 回答
如何添加身份验证/安全性以使用happybase访问HBase?
8 回答