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

查看:11
本文介绍了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

伊恩

推荐答案

您应该能够对提供的注册表单进行子类化并覆盖 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天全站免登陆