简体   繁体   English

如何在Django中实现具有不同角色和权限的多种用户类型?

[英]How to implement multiple user types with different roles and permissions in Django?

I'm new to web development using Django Framework and Python programming language and I am tasked to make a mini project that implements the following: 我是使用Django框架和Python编程语言进行Web开发的新手,我的任务是制作一个实现以下目标的迷你项目:

  1. Permissions Management; 权限管理;
  2. Roles Management; 角色管理;
  3. Users Accounts Management; 用户帐户管理;
  4. Login and Logout; 登录和注销; and
  5. Password Reset. 重设密码。

What are the appropriate steps for me to accomplish this? 我可以采取哪些适当的步骤来完成此任务?

I have read about user authentication and permissions and custom user models but the examples that were given were less complicated than what I actually needed. 我已经阅读了有关用户身份验证和权限以及自定义用户模型的信息,但是给出的示例没有我实际需要的复杂。 I have to set up an application that implements multiple user accounts wherein each user can have multiple roles and each roles have different permissions. 我必须设置一个实现多个用户帐户的应用程序,其中每个用户可以具有多个角色,并且每个角色具有不同的权限。 I know that Django has built-in functions to do this but I want to make my own customization as mush as possible. 我知道Django具有内置功能来执行此操作,但是我想尽可能地进行自己的自定义。

I would like to know if creating Custom User Model extending AbstractBaseUser is the best option there is to accomplish this. 我想知道是否创建扩展AbstractBaseUser的自定义用户模型是实现此目的的最佳选择。 Also, I would like to know how to customize the Django built-in admin page, such as replacing or deleting unnecessary fields. 另外,我想知道如何自定义Django内置管理页面,例如替换或删除不必要的字段。

the other workaround except extending AbstractBaseUser is to have another model Profile and relate to it with an OneToOneField or you can inherit 3 different user models from AbstractBaseUser which is not a good idea the best answer is to extend AbstractBaseUser if default authentication backend and perimissions doesn't suit your needs checkout(which they most likely do!): 除了扩展AbstractBaseUser以外,另一种解决方法是拥有另一个模型Profile并与OneToOneField关联,或者您可以从AbstractBaseUser继承3个不同的用户模型,这不是一个好主意,最好的答案是,如果默认身份验证后端和权限不对,则扩展AbstractBaseUser。不适合您的需求结帐(他们很可能会这样做!):

Customizing authentication in Django 在Django中自定义身份验证

Custom users and permissions 自定义用户和权限

also checkout: 还结帐:

django best approach for creating multiple type users django创建多个类型用户的最佳方法

How to Implement Multiple User Types with Django 如何使用Django实现多种用户类型

--UPDATE-- -更新-

and if you want to have different permission groups checkout: 并且如果您要使用其他权限组签出:

Permissions and Authorization 权限和授权

Groups 团体

django.contrib.auth.models.Group models are a generic way of categorizing users so you can apply permissions, or some other label, to those users. django.contrib.auth.models.Group模型是对用户进行分类的通用方法,因此您可以向这些用户应用权限或其他标签。 A user can belong to any number of groups. 用户可以属于任意数量的组。

A user in a group automatically has the permissions granted to that group. 组中的用户自动拥有授予该组的权限。 For example, if the group Site editors has the permission can_edit_home_page , any user in that group will have that permission. 例如,如果站点编辑器组具有can_edit_home_page权限,则该组中的任何用户都将具有该权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM