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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:54:51+00:00 2026-06-10T09:54:51+00:00

Possible Duplicate: Validating a form with overloaded init I’ve spent the entire afternoon trying

  • 0

Possible Duplicate:
Validating a form with overloaded init

I’ve spent the entire afternoon trying to figure out what I’m doing wrong here, and I know it’s going to be something face-palmingly stupid. When the user fills out a form, the location selection box depends on the country of the user and lists different cities accordingly.

Model & Modelform

class Cars(models.Model):
    owner = models.ForeignKey(User)
    name = models.CharField(max_length=25)
    location = models.PositiveIntegerField(default=1)
    created = models.DateTimeField(default=utc_time)

class CarsForm(ModelForm):
    def __init__(self, country, *args, **kwargs):
        super(CarsForm, self).__init__(*args, **kwargs)
        self.fields['location'] = forms.ModelChoiceField(queryset=Cities.objects.filter(country=country))

    class Meta:
        model = Cars
        exclude = ('owner', 'created',)

View

def new_car(request):
    if request.method == u'POST' and request.is_ajax():
        form = CarsForm(request.POST)
        if form.is_valid():
                      etc...

Following advice from stackoverflow, I edited the modelform init to allow the country variable to be passed. The form creation and html displays fine and everything works great, only now the form won’t validate.

UPDATE:
Ok, it looks like I (meaning ‘YOU’) got it working. Several things were ugly, and once fixed it works ok:

  • the location field type was changed to foreignkey, correcting the
    data mismatch issue
  • the init was revised to accept country as a kwarg
  • the form validation now passes the variable as a kwarg
    CarsForm(request.POST, country=country)

SUCCESS! In under an hour. Thanks so much everyone!!

  • 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-10T09:54:52+00:00Added an answer on June 10, 2026 at 9:54 am

    Either this is the solution, or your copy-and-pasting has problems ;)

    You’ve correctly defined the over-ridden __init__ method, but you’re not actually passing a country parameter; doing so would look like this:

    form = CarsForm(country, request.POST)
    

    This means that request.POST always gets stored in country and never passed on to the parent class’s __init__ method.

    If the form gets no data, is_valid() will always return False.

    A side-note: where does SkillForm come from? Should be CarsForm.

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

Sidebar

Related Questions

Possible Duplicate: MVC3 custom validation: compare two dates I am trying to make a
Possible Duplicate: Compare 2 dates with JavaScript Hi, I'm working on form validation for
Possible Duplicate: What is the best regular expression for validating email addresses? I want
Possible Duplicates: What’s wrong with this RegEx for validating emails? How to verify if
Possible Duplicate: E-mail validation in php? please help I have Ajax sign up form.
Possible Duplicate: How to use Twitter Bootstrap popovers for jQuery validation notifications? Trying to
Possible Duplicate: What is the best regular expression for validating email addresses? I am
Possible Duplicate: HTML5 form required attribute. Set custom validation message? in HTML 5 form
Possible Duplicate: PHP validation/regex for URL Hi, in my form I have a url
Possible Duplicate: HTML5 form required attribute. Set custom validation message? Simple_form has an automatic

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.