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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:45:23+00:00 2026-06-09T16:45:23+00:00

I have a form that gets values from a database created by a model.

  • 0

I have a form that gets values from a database created by a model. Say my the table has 2 columns, city and code, and I display just the city in my form using a ModelChoiceField.

When the use submits the form and I am going through the validation process, I would like to change the value of the city the user has selected with it’s code.

models.py

class Location(models.Model):
    city                = models.CharField(max_length=200)
    code                = models.CharField(max_length=10)

    def __unicode__(self):
        return self.city

forms.py

city = forms.ModelChoiceField(queryset=Location.objects.all(),label='City')

views.py

def profile(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():

            ???????

How could I do this?

Thanks – Oli

  • 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-09T16:45:25+00:00Added an answer on June 9, 2026 at 4:45 pm

    You can do this:

    def profile(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            profile = form.save(commit=False)
    
            #Retrieve the city's code and add it to the profile
            location = Location.objects.get(pk=form.cleaned_data['city'])
    
            profile.city = location.code
            profile.save()
    

    However you should be able to have the form setting the code directly in the ModelChoiceField. Check here and the django docs

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

Sidebar

Related Questions

I have a form that has all the fields pulled dynamic from a database
I have the following php array that gets all values from a form full
I have a code in php/mysql that gets ids from the database. The ids
I have a form that gets populated with data from the database. Before is
I have created a list form that gets attached to a main form in
I have a form that has label values that I would like to pass
I have a table (Dataview) with content from a database, where each row/object has
Hi I have the following view that gets the values for the form fields
I have a method, -(void)validateFormInput:(UITextField *)textField that gets called when the form's submit button
I currently have form that checks if a user has unsubmitted changes when they

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.