gpt4 book ai didi

python - Django:使用户电子邮件成为必需的

转载 作者:太空狗 更新时间:2023-10-29 21:51:51 26 4
gpt4 key购买 nike

对于我的应用程序,用户的电子邮件字段应该是必需的。默认用户模型不是这种情况。所以我认为创建自定义用户模型是有意义的,如 docs 中所述。 :

If you’re starting a new project, it’s highly recommended to set up a custom user model, even if the default User model is sufficient for you. This model behaves identically to the default user model, but you’ll be able to customize it in the future if the need arises:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
email = models.EmailField(_('email address'), blank=False)

但是,我不确定这是否是实现此目标的正确方法。

原因是here Django 文档说:

Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class. It is an error to have fields in the abstract base class with the same name as those in the child (and Django will raise an exception).

我现在该怎么办?

最佳答案

我必须回答我的问题,因为现在我知道解决方案了:

我在开始帖子中描述的方式应该有效。文档是错误的: https://code.djangoproject.com/ticket/29192

关于python - Django:使用户电子邮件成为必需的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49134831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com