Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7583079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:35:45+00:00 2026-05-30T18:35:45+00:00

What is the difference between, say, a EmailField and a validator_email ? And is

  • 0

What is the difference between, say, a EmailField and a validator_email? And is it a bad idea to use both?

Or for those who perfer code

import django.db import models
email = models.EmailField()

vs

import django.db import models
email = models.CharField( max_length=75, validators = validate_email )

From the doc it seems like you could also use validators inside forms but if you already specify a validation restriction inside models.py, then you don’t need specify again in the forms, right? So it seems better to me to take care of all of the restriction inside models.py.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:35:47+00:00Added an answer on May 30, 2026 at 6:35 pm

    I suppose the difference is very little, but then you would be violating the DRY principal, which you probably shouldn’t do, unless you have a good reason to do it.

    If you go to the code base:

    #django.db.fields.__init__.py
    
    class EmailField(CharField):
        default_validators = [validators.validate_email]
        description = _("E-mail address")
    
        def __init__(self, *args, **kwargs):
            kwargs['max_length'] = kwargs.get('max_length', 75)
            CharField.__init__(self, *args, **kwargs)
    
        def formfield(self, **kwargs):
            # As with CharField, this will cause email validation to be performed
            # twice.
            defaults = {
                'form_class': forms.EmailField,
            }
            defaults.update(kwargs)
            return super(EmailField, self).formfield(**defaults)
    

    As you can see, the model inherits from Charfield, so you lose nothing by using emailfield, where appropriate. Furthermore, the default validator is validate_email. Additionally you get the description variable already defined for you. Lastly, on the backend it is already setting max_length for you at ’75’. You could of course override this easily enough by defining a max_length in the same way you would when creating a CharField.

    You can see formfields() is returning forms.EmailField from django.forms.

    Looking at that, you can see:

    #django.forms.fields.py
    class EmailField(CharField):
        default_error_messages = {
            'invalid': _(u'Enter a valid e-mail address.'),
        }
        default_validators = [validators.validate_email]
    
        def clean(self, value):
            value = self.to_python(value).strip()
            return super(EmailField, self).clean(value)
    

    However, you would lose any default values that using the EmailField might provide, such as the “correct” error message and the custom clean() method.

    In the end, while it looks small, actually a good bit of work has already been done for you. So, in general, you shouldn’t violate the DRY principal unless you have a good reason to do so.

    Edit:

    Regarding the second question, you want the form to validate against whatever criteria you are concerned about, so when you call form.is_valid() it returns True / False when it should and generates the appropriate failure message. Otherwise, is_valid() would validate True, and when you model goes to save, it would fail silently, which would be very hard to track down.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have the below code. What is the difference between assigning the
What is the difference between charsets and character encoding? When i say i am
Can any one say difference between the iPhone OS 2+ till 4 and what's
In Objective-C what is the difference between defining something (say a property or a
What is the difference between event.target and this ? Let's say I have $(test).click(function(e)
Let's say I have a method called foo. What's the difference between: [self foo];
Let's say we have a class name Home. What is the difference between Home.this
In python, is there a difference between say: if text == 'sometext': print(text) if
Is there a speed difference between, say: $newstring = $a and $b went out
What's the difference between file and open in Python? When should I use which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.