简体   繁体   English

在Django中处理多种类型的用户的最佳方法

[英]Best way to handle multiple type of users in Django

In my project I have to deal with different type of users eg costumers and employers . 在我的项目中,我必须处理不同类型的用户,例如客户雇主 Each type of user have its own fields and permissions: costumer can buy things whereas employer can not. 每种类型的用户都有自己的字段和权限:客户可以购买商品,而雇主不能。

I have read the Django docs and it looks like there are two options: 我已经阅读了Django文档,并且看起来有两个选择:

  1. Costumize the AbstractUser class and adding all the fields of costumers and employers. AbstractUser类进行汇总,并添加客户和雇主的所有字段。 Then use the permission system to grant/revoke permission or create a group for each type of user. 然后,使用权限系统授予/撤消权限,或为每种类型的用户创建一个组。 The downside here is that there unused fields. 这里的缺点是有未使用的字段。
  2. Adopt the proxy model : 采用代理模式

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

    \n\n

    class Costumer(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) Costumer(models.Model)类:user = models.OneToOneField(User,on_delete = models.CASCADE)

    \n\n
     class Meta: db_table = 'costumers' permissions = ( ("access_this", "User may access this"), ) ordering = [] 
    \n\n

    class Employee(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) Employee类(models.Model):user = models.OneToOneField(User,on_delete = models.CASCADE)

    \n\n
     class Meta: db_table = 'employers' permissions = ( ("access_that", "User may access that"), ) ordering = [] 
    \n\n

    This case seems more reasonable but I don't know how to deal with the permissions. 这种情况似乎更合理,但我不知道如何处理权限。 Consider I'd like to use @permission_required instead of checking the type (if the user has a specific field) because it seems more legit for Django system. 考虑一下,我想使用@permission_required而不是检查类型(如果用户具有特定字段),因为这对于Django系统而言似乎更合法。

So in the end, what is the best way to approach such scenario? 那么,最后,解决这种情况的最佳方法是什么?

The first solution is much better. 第一个解决方案要好得多。

The downside here is that there unused fields. 这里的缺点是有未使用的字段。

I disagree with this, you don't have to store all the fields within the User model. 我不同意这一点,您不必在User模型中存储所有字段。 Also, if you're talking about 5 fields for example, that doesn't really matter. 另外,如果您要谈论的是5个领域,那也没关系。


You can extend AbtractUser and also use some composition; 您可以扩展AbtractUser ,也可以使用一些组合。 you don't have to put all the fields there: 您不必将所有字段都放在这里:

class User(AbstractUser):
     email = ...
     ...
     # for the employer, let's say you want to save the company details
     company = models.ForeignKey('myapp.Company', null=True, blank=True)
     ...
     # for the customer
     customer_details = models.ForeignKey('...')

This way you could record a user_type if you want or deduce the type from the foreign key (if there is a company, it's an employer). 这样,您可以记录一个user_type如果您想要或从外键推导该类型(如果有公司,则是雇主)。


To help you more with the model, I need to know what differentiate an employer from a customer in your application. 为了在模型方面为您提供更多帮助,我需要知道在您的应用程序中雇主与客户有何区别。 Note that with that solution, an instance of User could be both. 请注意,使用该解决方案, User的实例可以是两者。


Concerning the permissions, I feel like it's a separate problem; 关于权限,我觉得这是一个单独的问题。 I'd recommend you to sort it last. 我建议您将其排序。 If the design you pick is close to the reality and you get the features working; 如果您选择的设计接近现实,并且您可以使用功能; adding custom permissions will be really easy. 添加自定义权限将非常容易。

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

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