Django用户模型电子邮件字段:如何使其强制 [英] Django User model email field: how to make it mandatory

查看:139
本文介绍了Django用户模型电子邮件字段:如何使其强制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使Django用户模型中的电子邮件字段成为必需。对我来说这不是很明显。建议欢迎。我目前正在使用:

I need to make the email field in the Django User model mandatory. It isn't obvious to me how to do that. Suggestions welcome. I am currently using:

from django.contrib.auth.forms import UserCreationForm

为我的用户创建表单,并将其与我自己的自定义 UserProfileCreateForm

for my User creation form, and combining this with my own custom UserProfileCreateForm

Ian

推荐答案

您应该可以对提供的注册表进行子类化,并覆盖来自django.contrib.auth的 Meta 类中的字段。

You should be able subclass the provided registration form and override properties of a field in the Meta class.

from django.contrib.auth.forms import UserCreationForm

# Not sure about the syntax on this one. Can't find the documentation.
class MyUserCreationForm(UserCreationForm):

    class Meta:
        email = {
            'required': True
        }


# This will definitely work
class MyUserCreationForm(UserCreationForm):

    def __init__(self, *args, **kwargs):
        super(MyUserCreationForm, self).__init__(*args, **kwargs)

        self.fields['email'].required = True

这篇关于Django用户模型电子邮件字段:如何使其强制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆