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

The Archive Base Latest Questions

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

In my model I have a field which should contain only the values ‘A’,

  • 0

In my model I have a field which should contain only the values ‘A’, ‘B’ and ‘C’. Would this be best use for using a choice parameter when declaring the field?

If I didn’t decide to use the choice parameter and wanted to write custom validation logic for it, where would I write this — would this be in the clean method of the model? I’ve also seen clean_<fieldname> methods — what are these for or doe they only apply to forms? I would like to do this validation in the model as I’m not using a form.

class Action(models.Model):
    """
    Contains the logic for the visit
    """
    id = models.AutoField(primary_key=True)
    path = models.CharField(max_length=65535, null=False)
    to = models.IntegerField(null=False)

    def clean(self, **kwargs):
        """
        Custom clean method to do some validation
        """
        #Ensure that the 'to' is either 1,2 or 3.
        if self.to not in [0, 1, 2]:
            raise ValidationError("Invalid to value.")

When I’m doing validation, do I need to return some value? Will my method method be called when someone creates a new record?

(Although I’ve read the docs, I’m still a little confused about this.)

Thanks a ton.

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

    In the example you gave, I would use the choice parameter. If you put the validation for to field in the clean method, then any errors will be associated with the action instance and not the to field.

    As you stated, clean_<fieldname> methods are for form fields. On the model, you can define validators.

    Here’s an example of your clean method rewritten as a validator.

    from django.core.exceptions import ValidationError
    
    def validate_to(value):
        """
        Ensure that the 'to' is either 1, 2 or 3.
        """
        if value not in [1, 2, 3]:
            raise ValidationError("Invalid 'to' value.")
    
    class Action(models.Model):
        """
        Contains the logic for the visit
        """
        id = models.AutoField(primary_key=True)
        path = models.CharField(max_length=65535, null=False)
        to = models.IntegerField(null=False,validators=[validate_to])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model Foo which has field bar. The bar field should be unique,
Say I have a model object 'Person' defined, which has a field called 'Name'.
I have an ActiveRecord model, Foo , which has a name field. I'd like
I have model Article it has field title with some text that may contain
I have a database table with a certain field which should be impossible to
I have a pretty standard scenario where I have a model which in this
I have an object $this->user which is of model User . This object is
I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
I have a basic ActiveRecord model in which i have two fields that i
I have a basic model in which i have specified some of the fields

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.