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 6082219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:15:52+00:00 2026-05-23T11:15:52+00:00

I have a few questions about validation in Models and Forms. Could you help

  • 0

I have a few questions about validation in Models and Forms. Could you help me out with these:


Where should validation be done? Should it be in the Model or the Form? Is the right way to go about this is to have validators in the form and constraints in the mode?


What is the difference between writing a ‘clean_’ method in the form and writing a validator? I’ve seen that people often put validation checks in the ‘clean_’ method.


In the request that I’m handling, I have a param in the URL string called ‘alive’. This is generally 1 or 0. What would be the correct way of defining this in my form? I need to validate it is a number and can only be 1 or 0. Is this the right way?

alive = models.IntegerField(null=False, max_value=1, min_value=0)

How do I define a default value for this field i.e. if this parameter isn’t passed, I default to 0 (False).

I don’t have a form on the client side. I’m using the Django form the validate my JS POST request.


In one of model fields I need to store screen resolution in the format 1234×4321. Should I declare this as a CharField add some regular expression validation in both the Model and the Form? Any examples of regular expression validations would be helpful.


Thanks.

  • 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-23T11:15:53+00:00Added an answer on May 23, 2026 at 11:15 am

    The validation should be done on the form, not the model. However, if you are using ModelForms, which is usually makes a lot of sense, it will inherit some of the validation rules from the models themselves (those specific to the database, like maximum_field length, database field type, but also if they can be left blank).

    The default value of a field should be passed with its constructor:

    form = SomeForm(initial={'alive' : 0})
    

    Although in your case, it appears that if the values that can be obtained are only zero and one, it would be make sense to use a BooleanField instead(tand in that case it would default to false).

    In the case of resolutions I would create a mapping between the possible resolution and some arbitrary value.

    RESOLUTIONS = (                                                
            ("1","800x600"),
            ("2","1024x768"),                            
             .....                        
    )   
    

    and then pass it to the model:

    resolutions = models.CharField(RESOLUTIONS, max_length=1)
    

    So that the user gets a select field with the corresponding options and values.
    On the other hand, if you need the user to insert it him/herself, using two fields (one for width, another for height) would be much easier than validating the user input.

    So you can define a method for the model:

    def get_resolution(self):
        return "%sx%s" % (self.width, self.height)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few questions about Core Location. 1) Should the user refuse permission
I have a few questions about Apartments: Was this model introduced for COM purposes
I have a few questions about storing files on the operating system. These may
i have a few questions about flash web sites. are their pages done using
I have a few questions about data synchronization. The architecture does not seem to
I have a few questions about Xcode and interaction with GCC 4.2.1: It doesn't
I have a few questions about this after reading the iPhone documentation on it:
I have a few questions about cross-page posting in ASP.NET: What is cross-page posting
I have a few quick questions about the Microsoft Detours Library. I have used
I've been hearing about triggers, and I have a few questions. What are triggers?

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.