如何更新标准Django模型(auth_user)

时间:2020-12-31 19:17:58

I would like to add new field into existing model (in this case - User(auth_user)). For example, there are fields "first_name", "last_name", "email".. etc. Please advise how best to implement this?

我想在现有模型中添加新字段(在本例中为User(auth_user))。例如,有“first_name”,“last_name”,“email”等字段。请告知如何最好地实现这一点?

1 个解决方案

#1


You can extend or create you own substitute User model. In my point of view is better to create your own.

您可以扩展或创建自己的替代用户模型。在我看来,最好是创建自己的。

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

class User(AbstractUser):
    my_field = models.CharField(max_length = 150)

and then in your settings.py

然后在你的settings.py中

AUTH_USER_MODEL = 'myapp.User'

I hope this help you.

我希望这对你有帮助。

#1


You can extend or create you own substitute User model. In my point of view is better to create your own.

您可以扩展或创建自己的替代用户模型。在我看来,最好是创建自己的。

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

class User(AbstractUser):
    my_field = models.CharField(max_length = 150)

and then in your settings.py

然后在你的settings.py中

AUTH_USER_MODEL = 'myapp.User'

I hope this help you.

我希望这对你有帮助。