如何在Django中正确扩展用户模型

huangapple go评论66阅读模式
英文:

How to properly extend user model in Django

问题

我正在开发一个Django员工管理系统,我希望员工的个人信息数据能够与用于注册和登录的用户对象在同一表中。

我尝试将用户模型添加为员工表中的OneToOneField,但我不喜欢用户需要先注册,然后单独填写员工表单。

我希望用户在创建帐户的同时能够创建他们的个人信息。

英文:

I am working on a Django employee management system, I want the employee bio data on the same table as the user object used for registration and login

I tried adding the user model as a OnetoOneField on the employee table, but I don’t like having to register the user and then fill the employee form separately.

I want the users to be able to create their bio data while creating an account

答案1

得分: 1

你可以继承AbstractBaseUser模型并覆盖它(在其中添加额外字段),然后你可以完全按照你的要求进行操作,即:

class User(AbstractBaseUser):
    date_joined = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    # 其他字段
英文:

you can inherit AbstractBaseUser model and override it(add additionally fields in it) and you can do what exactly you want i.e

class User(AbstractBaseUser):
    date_joined = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    ---> rest of fields <------

答案2

得分: 0

数据库设计可能因人而异。在这种情况下,我会继承AbstractBaseUser,并将所有内容保存在该表中。

英文:

database design may always vary depending on people. In such a case, I will inherit abstractbaseuser and keep everything in that table.

huangapple
  • 本文由 发表于 2023年3月4日 04:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定