简体   繁体   English

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

[英]django best approach for creating multiple type users

I want to create multiple users in django. 我想在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)
    .......

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...?? 或者我必须制作自定义用户模型...这对于创建多个类型的用户来说会很好......

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. 所以,从一开始 - 在django中只有一个用户类型。 If you use the default authentication framework, the model for this user is called User , from django.contrib.auth.models . 如果使用默认身份验证框架,则此用户的模型称为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. 您可以针对facebook / google等进行身份验证或针对现有用户数据库进行身份验证 - 例如,如果您使用的是Windows网络,则使用ActiveDirectory。

  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". 默认情况下,在每个模型上 - django将添加基本权限“可以编辑”,“可以删除”,“可以读取”。 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. 您可以存储有关用户的额外信息,以及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. 如果django默认提供的所有内容都适合您,而您想要做的就是存储有关用户的额外信息,您可以扩展用户模型 - 在以前的版本中,这称为创建自定义配置文件。 The other option you have is to create your own User model , if you want deeper customization. 如果您想要更深入的自定义,您拥有的另一个选择是创建自己的User模型 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. 在某些应用程序中,您必须修改所有三个位置。

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 ): 在您的情况下,由于您要做的只是存储有关用户的额外信息 ,您需要通过创建引用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.

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.pysettings.py用于身份验证的模型:

AUTH_USER_MODEL = 'your_app.User'

Also note that you will now have to use settings.AUTH_USER_MODEL when referencing your new User model in a relation in your models. 另请注意,在模型中的关系中引用新User模型时,您现在必须使用settings.AUTH_USER_MODEL

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

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

相关问题 在Django中处理多种类型的用户的最佳方法 - Best way to handle multiple type of users in Django 多个Django项目和异步电子邮件的最佳方法 - Best approach for multiple Django projects and async emails 在 Django 中创建一个简单的多用户应用程序 - Creating a simple multiple users app in django 在 Django 中创建新用户返回:“用户类型的对象不是 JSON 可序列化的” - Creating a new user in Django returns: “Object of type Users is not JSON serializable” 保持许多不同的Django模型类型选择为DRY的最佳方法是什么? - What is the best approach to keep many different django model type choices DRY? Django - 处理多种用户类型的最佳方法是什么……并基于此路由 HTML 页面? - Django - What is the best approach to handle multiple user types…and route the HTML pages based on this? 如何在 Django 中使用 google OAuth 创建多种类型的用户 - How to create multiple type of users using google OAuth in Django 在 Django 网站上处理大文件的最佳方法 - Best approach to handle large files on django website 用Django实现配置应用的最佳方法是什么? - What is the best approach to implement configuration app with Django? 在 Django + SQL 中使用多对多的最佳方法 - Best approach to use Many to Many in Django + SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM