简体   繁体   English

扩展用户模型 Django 2.0

[英]Extend User Model Django 2.0

I'm starting a whole new project using Django 2.0 and python, so I'm at the beginning of deciding how to implement the Multiple User Types.我正在使用 Django 2.0 和 python 开始一个全新的项目,所以我正在决定如何实现多用户类型。

What I've read so far is that I can extend the User built-in model for django so that I would get use of django's authentication process, and create another models that links one-to-one with that user model.到目前为止我读到的是,我可以扩展 django 的 User 内置模型,以便我可以使用 django 的身份验证过程,并创建另一个与该用户模型一对一链接的模型。 But actually I can't understand a little bit.但其实我有点看不懂。

My application has three user types: Participant, Admin, Judge, each of them will view certain pages(templates) and as well as permissions.我的应用程序有三种用户类型:参与者、管理员、法官,他们每个人都会查看某些页面(模板)以及权限。

Can someone provide me with the best practice/approach to start working on those user types.有人可以为我提供开始处理这些用户类型的最佳实践/方法吗?

Note: In the future, each user may have different fields than the other, for ex.注意:将来,每个用户可能有不同的字段,例如。 Judge may have Join date while participant won't...etc法官可能有加入日期而参与者不会......等

If you haven't already, the documentation seems to mention what you are trying to do.如果您还没有, 文档似乎提到了您正在尝试做的事情。

Here is an example which creates custom user models.这是一个创建自定义用户模型的示例 But I think you are essentially right.但我认为你基本上是对的。 You can create an abstract base user with the standard information (name, email, etc).您可以使用标准信息(姓名、电子邮件等)创建抽象基本用户。 Then you can create a separate class which is just a model class, then set a foreign key to your abstract base user, and add additional data for that user.然后您可以创建一个单独的类,它只是一个模型类,然后为您的抽象基本用户设置一个外键,并为该用户添加其他数据。

class User(AbstractBaseUser):
    email = models.EmailField(max_length=255, unique=True)
    # etc

class Judge(models.Model):
    user = models.ForeignKey(User)
    # User specific data

Hope this helps.希望这可以帮助。

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

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