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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:28:33+00:00 2026-06-13T22:28:33+00:00

I’m using Django 1.4 formsets and I’d like to perform different validation for the

  • 0

I’m using Django 1.4 formsets and I’d like to perform different validation for the first form in the formset.

I have a LocationForm where I display 4 text fields allowing the user to enter up to 4 locations:

class LocationForm(forms.Form):
    user_entered_address = forms.CharField(required=False, max_length=255)
    full_address = forms.CharField(max_length=255, required=False, widget=forms.HiddenInput())
    city = forms.CharField(max_length=255, required=False, widget=forms.HiddenInput())
    state = forms.CharField(max_length=255, required=False, widget=forms.HiddenInput())
    country = forms.CharField(max_length=255, required=False, widget=forms.HiddenInput())
    latitude = forms.CharField(required=False, widget=forms.HiddenInput())
    longitude = forms.CharField(required=False, widget=forms.HiddenInput())

LocationFormSet = formset_factory(LocationForm, extra=4, max_num=4)

This works great to display the input fields, but I would like the first LocationForm in the formset to be required and the rest of the forms to be optional (meaning the user must enter at least 1 location in the first form, but the rest are optional).

If I modify the form, removing required=False:

    user_entered_address = forms.CharField(max_length=255)

Then user_entered_address is required for all forms. I have a feeling I need to write a custom clean() method, but I’m stuck how to find the first form, figure out if user_entered_address is blank and if so, raise a ValidationError:

class BaseLocationFormSet(BaseFormSet):
    def clean(self):
        # How to do I find the first form and ensure user_entered_address isn't blank?

LocationFormSet = formset_factory(LocationForm, extra=4, max_num=4, formset=BaseLocationFormSet)

Or is this the wrong approach and there is a better way to do this? 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-06-13T22:28:34+00:00Added an answer on June 13, 2026 at 10:28 pm

    You can access the forms like:

    form = self.forms[0]
    

    So in your clean just check the first form and ignore the others.

    form = self.forms[0]
    title = form.cleaned_data['title']
    if title is None:
        raise forms.ValidationError("Missing title")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.