Can the User model be overridden to make the email field required?

yogeshiitm

For my application, the email field in the Django User model should be required, but by default it's optional.

I searched this question but most answers I found were talking about making the email field required in the customised UserCreationForm for example:

class CustomUserForm(UserCreationForm):
    """Provide a view for creating users with only the requisite fields."""

    class Meta:
        model = User
        # Note that password is taken care of for us by auth's UserCreationForm.
        fields = ('username', 'email')

    def __init__(self, *args, **kwargs):
        super(CustomUserForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True

but I want to make the email mandatory in the User model as well because I was thinking maybe ModelForm are a frontend thing and the required condition maybe be turned off by the user by using the chrome dev tools, am I wrong? Will making the email required in ModelForm is same as making it required in the user model as well, or can I override the user model to make the email required?

Willem Van Onsem

Maybe ModelForm are a frontend thing and the required condition maybe be turned off by the user by using the chrome dev tools, am I wrong?

You are correct that a user can use tools to make an input item not required, in fact you can often alter the DOM and thus alter the HTML form, or even more simply make use of a tool like curl to make a POST request where you can post arbitrary data.

But a Form (and by extent an ModelForm) does not only render the form in HTML, it is also used to validate the data. This thus means that if you use a view with:

def my_view(request):
    if request.method == 'POST':
        form = CustomUserForm(request.POST, request.FILES)
        if form.is_valid():
            # …
        # …
    # …

then the form.is_valid() will return False if the user did not fill in an email address. A form is thus used to validate, clean data, store data in a model, and render a HTML form.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to make email field required in the django user admin

Django: Make user email required

How to make Required: boolean field in model Django

How can I make reCAPTCHA a required field?

Login user for a custom user model with email as username field

Make a model field as required on the base of a condition in Django Models

Extract email field from 'User' model and add it to a seperate model 'Email' via foreign key

make hidden field required

If checkbox is checked then email field not required

Updating model - email is required to be unique

The email field is required but no input email in view Laravel

Can I make an admin field not required in Django without creating a form?

How can I make a DateTime model property required?

How to make not required the email in Prestashop

User validation failed: email: Path `email` is required

Django required field in model form

Required field in Django model not mandatory?

Laravel Required Field on Model Update

Django model form this field is required

How to make email permission required when logging in/signing up a user using the FB login?

Kentico - How to make a field required?

How to make a field required in Sequelize?

Cannot make Byte a Required Field

How to make a field required in Rails?

Can I make the foreign key field optional in Django model

Can I make at least one field a requirement on a Django Model?

Laravel 5.5 The email and password field is required

login username or email field required without validation

try To make a Input Text Field in flutter But I can't Make a validator function that send as required function