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

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

问题描述

我想在Django中创建多个用户。我想知道哪种方法最好。.

I want to create multiple users in django. I want to know which method will be the best..

class Teachers(models.Model):
    user = models.ForeignKey(User)
    is_teacher = models.BooleanField(default=True)
    .......

还是我应该使用。.

class Teacher(User):
    is_teacher = models.BooleanField(default=True)
    .......

或我必须创建自定义用户模型...
这将对创建多种类型的用户有好处... ??

or I have to make custom user model... Which will be good on creating multiple type users...??

推荐答案

Django没有多个用户-它只有一个用户,然后根据权限用户可以执行不同的操作。

Django doesn't have multiple users - it only has one user and then based on permissions users can do different things.

因此,首先- django中只有一种用户类型。如果使用默认的身份验证框架,则此用户的模型称为 User ,来自 django.contrib.auth.models

So, to start off with - there is only one user type in django. If you use the default authentication framework, the model for this user is called User, from django.contrib.auth.models.

如果要在Django中自定义用户行为,可以执行以下三项操作:

If you want to customize user behavior in django, there are three things you can do:


  1. 自定义验证方式 。默认情况下,使用存储密码的数据库完成身份验证。您可以通过facebook / google等进行身份验证,也可以通过现有的用户数据库进行身份验证-例如,如果您使用的是Windows网络,则可以使用ActiveDirectory进行身份验证。

  1. Customize how you authenticate them. By default, authentication is done using a database where passwords are stored. You can authenticate against facebook/google etc. or against your existing user database - for example, with ActiveDirectory if you are on a Windows network.

创建自定义权限,并基于这些权限来限制用户可执行的功能。默认情况下,在每个模型上-django将添加基本权限可以编辑,可以删除,可以读取。您可以创建自己的,然后检查用户是否具有这些特定权限。

Create custom permissions, and based on these permissions, restrict what functions users can execute. By default, on every model - django will add basic permissions "can edit", "can delete", "can read". You can create your own and then check if the user has these specific permissions.

您可以存储有关用户的其他信息,以及通常存储的其他信息。 django。有两种方法可以执行此操作,具体取决于您需要多少自定义。如果django默认情况下为您提供了一切,并且您要做的就是存储有关用户的其他信息,则可以扩展用户模型-在以前的版本中,这称为创建自定义配置文件。您拥有的另一个选择是创建自己的用户模型,如果您想进行更深入的自定义。自定义用户模型的最常见用法是如果您想使用电子邮件地址作为用户名。

You can store extra information about the user, along with whatever normally is stored by django. There are two ways to do this, depending on how much customization you need. If everything django provides by default works for you, and all you want to do is store extra information about the user you can extend the user model - in previous versions this was called creating a custom profile. The other option you have is to create your own User model, if you want deeper customization. The most common use of a custom user model is if you want to use an email address as the username.

您不必全部执行这三个操作,实际上有时您要做的就是存储一些额外的信息,或者让他们使用其电子邮件地址进行身份验证;在某些应用程序中,您必须修改所有三个位置。

You don't have to do all three, in fact sometimes all you want to do is store some extra information or have them authenticate using their email address; in some applications you have to modify all three places.

在您的情况下,由于您要做的只是存储有关用户的额外信息 ,您需要通过创建一个模型来扩展用户模型。引用 User (注意:您不是从 User 继承的):

In your case, since all you want to do is store extra information about a user, you would need to extend the user model, by creating a model that references User (note: you don't inherit from User):

class Profile(models.Model):
    user = models.OneToOneField(User)
    department = models.CharField(max_length=200, default='Computer Science')
    is_teacher = models.BooleanField(default=False)
    is_student = models.BooleanField(default=True)
    # .. etc. etc.

这篇关于django创建多个类型用户的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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