Django的1.8,多个定制用户类型 [英] Django 1.8, Multiple Custom User Types

查看:274
本文介绍了Django的1.8,多个定制用户类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Django的网站,将有4个类型的用户;

I am creating a Django site that will have 4 types of users;


  • 超级管理员:从本质上管理所有类型的用户

  • UserTypeA:可以登录网站,基本上都会有一些任意模型CRUD接口。也有额外的属性(具体信息,只有属于类型A用户)

  • UserTypeB:可以登录网站,并有专门属于用户的TypeB额外的信息,还可以创建和管理TypeC用户帐户

  • UserTypeC:可以登录网站,只有这属于TypeC用户额外的信息

注意到我打算创建一个自定义的界面,而不是使用内置的管理界面。什么是执行这一用户结构的最好方法?我想preFER使用Django的内置认证系统(用来管理密码盐和散列,会话管理等一起),但我将开放给使用一些其他身份认证系统,如果它是安全的,生产就绪。

Noting that I plan on creating a custom interface and not utilizing the built-in admin interface. What would be the best method for implementing this user structure? I would prefer to use Django's built-in authentication system (which manages password salting and hashing, along with session management etc), but I would be open to using some other authentication system if it is secure and production ready.

编辑:同时,我的计划是用1登录界面来访问网站,并利用权限和用户类型来决定将可在每个用户的仪表板是什么

Also, my plan is to use 1 log-in screen to access the site and utilize the permissions and user type to determine what will be available on each user's dashboard.

编辑:有没有可能通过使用一个应用程序为每个用户类型来实现这一点,如果是的话怎么会这样一个单一的登录屏幕完成

Would it be possible to accomplish this by using an app for each user type, and if so how would this be done with a single log-in screen.

推荐答案

首先,你不能让一个项目多个认证用户群。所以,你必须使用所提供的Django的用户认证和叉它多类型的用户。 Django的用户需要在注册过程中提供(尝试创造Django管理的用户)一些默认值。你可以做的是创建一个名为CustomUser模式,并从 AbstractUser 继承。这将使你的'CustomUser样板工程用户的默认。因为你从 AbstractUser 继承这个'CustomUser车型将拥有各个领域从原来的用户模型,然后你可以自己添加一些领域。您还需要在原来的用户模式不是默认的验证模型了该项目的 settings.py 文件来指定,这是你的新'CustomUser模式,将用于认证。看看下面code有助于。

First of all, you cannot make multiple authentication user base for a project. So you have to use the Django user authentication provided and fork it for multiple types of users. The Django user has some default values you need to provide during registration (try creating a user in Django Admin). What you can do is create a model called 'CustomUser' and inherit from AbstractUser. This will make your 'CustomUser' model the default for the project users. Because you inherit from AbstractUser this 'CustomUser' model will have every field from the original Users model, and then you can add some field on your own. You also need to specify in the settings.py file of the project that the original Users model is not your default authentication model anymore, it is your new 'CustomUser' model that will be used for authentication. See if the following code helps.

from django.db import models
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    type_choices = (
        ('SU', 'Super User'),
        ('A', 'User Type A'),
        ('B', 'User Type B'),
        ('C', 'User Type C'),
    )
    user_type = models.CharField(max_length=2,
                                 choices=type_choices,
                                 default='C')

class UserDetails(model.Model):
    type = models.OneToOneField('CustomUser')
    extra_info = models.CharField(max_length=200)

在上面的code您已经创建了CustomUser模式,用户可以像提供用户名,密码等基本信息这是Django的默认值。然后你选择它是一个用户类型并保存上的UserDetails模型,也有ForeignKey的关系,以新的身份验证模型的附加信息。你需要做的最后一件事是在 settings.py 文件。

In the above code you have created the CustomUser model where users can provide the basic info like username, password, etc. which is the default in Django. And then you select which user type it is and save your additional information on UserDetails model which also has ForeignKey relation to your new authentication model. One last thing you need to do is in the settings.py file.

AUTH_USER_MODEL ='index.CustomUser'

在这里首页是我的 CustomUser 模式中创建的应用程序。

Over here index is the app that my CustomUser model is created in.

希望这有助于。

这篇关于Django的1.8,多个定制用户类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆