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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:31:43+00:00 2026-06-06T15:31:43+00:00

I have 3 models linked with a foreign key: class Region(models.Model): name = models.CharField(max_length=20)

  • 0

I have 3 models linked with a foreign key:

class Region(models.Model):
    name = models.CharField(max_length=20)

class Zipcode(models.Model):
    zipcode =  models.IntegerField()
    name = models.CharField(max_length=20)
    region = models.ForeignKey(Region)

class Address(models.Model):
    street = models.Charfield(max_length=40)
    zipcode = models.ForeignKey(Zipcode)
    region = models.ForeignKey(Region)

In a next step I made a form based on the model Address.

Now I would like to automatically fill in the region field when the user fills in the zipcode field.

I know it is possible to calculate the values in the back-end etc, but is it possible to complete the region field almost ‘real-time’ when the user selects the zipcode field? In a way that it also visible in the form for the user?

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-06T15:31:45+00:00Added an answer on June 6, 2026 at 3:31 pm

    I think you’ll want some sort of ajax lookup for this. For example, javascript (I suggest jquery) would be set to do a ajax request whenever someone entered a zipcode. Perhaps if the zipcode textbox length is six, then the onChange event would request the url “/ajax/get_region/60604/” which would map to the view below and pass in the zipcode. The view might return a json response which would indicate the region for that zip code. Finally, in the javascript callback function could update the dialog box on your site to indicate the region.

    My recommendation is that the region would not even need to be posted back to the webserver…. the ajax lookup here would only be used for display purposes and then it would be calculated again during the POST. But, then again, I don’t know if you want the user to be able to manually enter both the region and the zipcode.

    views.py

    import json
    from django.http import HttpResponse
    
    def ajax_region_from_zip(request, zip):
    
        # do your logic to lookup the zipcode from the region
        region, lookup_success = get_region_form_zip(zip)        
    
        response = Response(content_type='application/json')
        response.write(json.dumps({"success":lookup_success, "region":region ))
        return response 
    

    Finally, if you do want the user to be able to edit both region and zipcode in the form, you’ll want to ensure that the region and the zipcode are always consistent and you may want to build a check in the form that the user submits. Also, if the user disables javascript, you’ll want to provide an error to the user if they select the wrong region. You could override the clean() method on the form that would also lookup the region from the zipcode. If it doesn’t match the region that was posted by the front-end form, it would return an error message.

    forms.py

    class AddressForm(forms.ModelForm):
        ....
        ....
        def clean(self):
            cleaned_data = self.cleaned_data
            zip = cleaned_data.get('zip', None)
            region = cleaned_data.get('region', None)
            region_lookup, lookup_success = get_region_form_zip(zip)
            if not region == region_lookup:
                forms.ValidationError("This zipcode is not in the given region.")
            return cleaned_data 
    

    Note, these code examples, haven’t been tested, but hopefully get the idea across.
    Hope this helps,

    Joe

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

Sidebar

Related Questions

I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
I have that situation: I have models Item, Region and Country. class Item(models.Model): name
I have three models linked by foreign keys: class One(models.Model): ... class Two(models.Model): one
I have two models , Task and TaskComment : class Task(models.Model): title = models.CharField(max_length
my model is basically a chain of objects linked by a foreign key: class
I have 2 models class Vhost(models.Model): dns = models.ForeignKey(DNS) user = models.ForeignKey(User) extra =
I have these tables: class Contract(models.Model): project = models.ForeignKey(Project) start_date = models.DateField() agreed_contacts =
I have a Customer class linked to several other classes via foreign key. I
Say I have some django models, something like this: class Address(models.Model): pass class Person(models.Model):

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.