Python django 创建多种类型用户的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25841712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:43:42  来源:igfitidea点击:

django best approach for creating multiple type users

pythondjango

提问by gamer

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

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

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

or should I use..

或者我应该使用..

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...??

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

采纳答案by Burhan Khalid

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

Django 没有多个用户——它只有一个用户,然后根据权限用户可以做不同的事情。

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 中只有一种用户类型。如果您使用默认身份验证框架,则此用户的模型称为User,来自django.contrib.auth.models

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

如果你想在 django 中自定义用户行为,你可以做三件事:

  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.

  2. 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.

  3. 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 Usermodel, 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.

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

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

  3. 您可以存储有关用户的额外信息,以及 django 通常存储的任何信息。有两种方法可以做到这一点,具体取决于您需要多少自定义。如果 django 默认提供的所有内容都适合您,并且您想要做的就是存储有关用户的额外信息,您可以扩展用户模型- 在以前的版本中,这称为创建自定义配置文件。如果您想要更深入的定制,您拥有的另一个选择是创建自己的User模型。自定义用户模型最常见的用途是将电子邮件地址用作用户名。

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.

您不必同时做这三件事,事实上有时您想做的只是存储一些额外信息或让他们使用他们的电子邮件地址进行身份验证;在某些应用程序中,您必须修改所有三个位置。

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

在您的情况下,由于您想要做的就是存储有关用户的额外信息,因此您需要通过创建引用的模型来扩展 user 模型User(注意:您不继承自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.

回答by Nils Werner

One approach I was following with Django 1.7 (works with 1.6 too) is to subclass AbstractUser

我在 Django 1.7(也适用于 1.6)中遵循的一种方法是子类化 AbstractUser

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

class User(AbstractUser):
    balance = models.DecimalField(default=0.0, decimal_places=2, max_digits=5)

To use your model you need to set it to be the one used for authentication in settings.py:

要使用您的模型,您需要将其设置为用于身份验证的模型settings.py

AUTH_USER_MODEL = 'your_app.User'

Also note that you will now have to use settings.AUTH_USER_MODELwhen referencing your new Usermodel in a relation in your models.

另请注意,您现在必须在模型中的关系中settings.AUTH_USER_MODEL引用新User模型时使用。

from django.db import models
from django.conf import settings

class Transaction(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL) # ForeignKey(User) will not work