如何使用Django实现多种用户类型?

时间:2020-05-08 07:01:30

标签: python django django-models model

我想创建一个类似Django的模型

user_types = (("Agent", "Agent"), ("Branch", "Branch"), ("Company", "Company"))

class User(models.Model):
    email = models.EmailField(db_index=True, unique=True)
    username = models.CharField(db_index=True, max_length=255, unique=True)
    user_type = models.CharField(max_length=100, choices=user_types, blank=False)

,如果用户定义user_type来存储这些字段数据 应该有一些额外的字段,例如:

   if user_type is Agent then:
        agent_code
        agent_name
    if user_type is Branch then:
        Branch_code
        Branch_name
        Branch_Id
    if user_type is Company then:
        Company_code
        Company_name
        Company_Id

有什么好的方法来处理这个模型吗?

或者我必须在不同的modelsClass和固有的User类中定义所有必填字段。

喜欢:

class ReqData(models.Model):
    user_type = models.CharField(max_length=100, choices=user_types, blank=False)
    agent_code = models.CharField(max_length=50, blank=False)
    branch_code = models.CharField(max_length=50, blank=False)
    branch_name = models.CharField(max_length=50, blank=False)
    ....
    ....


class User(models.Model, ReqData):
    ....
    ....

0 个答案:

没有答案