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

  • Home
  • SEARCH
  • 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 6016419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:58:47+00:00 2026-05-23T02:58:47+00:00

I have this model and modelform: class Comment(models.Model): text = models.CharField(max_length=100) def clean_text(self): print

  • 0

I have this model and modelform:

class Comment(models.Model):
  text = models.CharField(max_length=100)

  def clean_text(self):
    print "called Comment"
    if len(self.text) <= 5:
      raise ValidationError("must have 5 chars at least")

class CommentForm(ModelForm):
  class Meta:
    model = Comment

  def clean_text(self):
    print "called CommentForm"
    if len(self.text) <= 5:
      raise ValidationError("must have 5 chars at least")

And I’m using them like this in the view:

  CommentFormSet = modelformset_factory(model=Comment,form=CommentForm,extra=3)
  if request.method == "POST":
    formset = CommentFormSet(request.POST)
    if formset.is_valid():
      print "VALID FORM"
  else:
    formset = CommentFormSet()
  return render_to_response("first.html",{"formset":formset},context_instance=RequestContext(request))

And this is my template:

<form action="first" method="post">
  {% csrf_token %}
  {% for dict in formset.errors %}
      {% for error in dict.values %}
          {{ error }}
      {% endfor %}
  {% endfor %}
  <table>
    {{ formset }}
  </table>
  <input type="submit" value="Create"/>
</form>

The thing is, my validation is never called. I have 3 comments which I can add at once, and if their text field is empty, django says it’s no problem. What am I not doing right?

EDIT: The variant with validator:

def validate_min(val):
  print "validator called"
  if len(val) <= 5:
    raise ValidationError("At least 5 characters should be provided")

class Comment(models.Model):
  text = models.CharField(max_length=100,validators=[validate_min])

My validator is not being called.

  • 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-23T02:58:48+00:00Added an answer on May 23, 2026 at 2:58 am

    As I know models don’t use clean_% methods. You should use validator.

    def validate_min_length(value):
        if len(value) <= 5:
            raise ValidationError("must have 5 chars at least")
    
    class Comment(models.Model):
        text = models.CharField(max_length=100, validators=[validate_min_length])
    

    Edited: the answer is simple: in formset forms with no data in all fields are ignored. That’s why formset is valid. Empty forms are created with empty_permitted=True. Which means they will pass validation. Override either of these and you’ll get what you want.

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

Sidebar

Related Questions

..whatdoyoucallitanyway.. I have this model: class Kaart(models.Model): name = models.CharField(max_length=200, name=Kaardi peakiri, help_text=Sisesta kaardi
This is my model: class News(models.Model): title = models.CharField(max_length=250) text = models.TextField() pub_date =
I have this: class OrderForm(ModelForm): class Meta: model = Order exclude = ('number',) def
I have a models A and B , that are like this: class A(models.Model):
I have a this model: class Fleet(models.Model): company = models.ForeignKey(Company, editable=False) aircraft = models.ForeignKey(Aircraft)
I have a ModelForm as follows: class ArticleForm(forms.ModelForm): title = forms.CharField(max_length=200) body = forms.CharField(widget=forms.Textarea)
I have a model like this: class MyModel(models.Model): REGULAR = 1 PREMIUM = 2
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base

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.