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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:38:03+00:00 2026-06-10T02:38:03+00:00

I’m trying to populate a django modelform with initial data provided from an external

  • 0

I’m trying to populate a django modelform with initial data provided from an external source. To achieve that I start by pull all the needed data from the external source:

url =('http://myapi.example.com')
data = urllib2.urlopen(url)
result = json.load(data)

api_data_name = result['properties']['name']
api_data_type = result['properties']['type']

Followed by populating a dict which will serve as initial data to my form:

data = {}
for field in my_model._meta.fields:
    if field.name == 'name':
        data[field.name] = api_data_name
form = MyEditForm(initial=data)

Then I’m passing the form to the template and the initial data is populating my text fields as expected, but now I need to be able to set a value of a select field based on a string I receive from my external source and Im not getting how can I achieve that, since doing something like:

if field.name == 'type':
    data[field.name] = api_data_type

Wont do the job cause the select element has “0”, “1”, “2”, etc as options value and not the long description i get from api_data_type variable.

How can I get the long_description from all the options <option value="1">long_description</option> of my select field in my view so i can compare each one with api_data_type?

Heres a sample of my models.py and forms.py:

#models.py

TYPE = (
    ('0',_(u'Type1')),
    ('1',_(u'Type2')),
    ('2',_(u'Type3')),
)

class MyModel(models.Model):
    ...
    type=models.CharField(max_length=30,choices=TYPE,blank=True)
    ...

#forms.py
class MyEditForm(forms.ModelForm):
    class Meta:
        model = MyModel

        widgets = {
            ...
            'type': Select(attrs={'class':'select-small span2'}),
            ...
        }
  • 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-10T02:38:05+00:00Added an answer on June 10, 2026 at 2:38 am

    Found out how to accomplish what I asked.

    # For select fields
    if field.name == 'classification':
        for choice in field.choices:
            if choice[1].lower() == api_poi_classification.lower():
                data[field.name] = choice[0]
    

    And for any of ya trying to populate many-to-many fields (as checkboxes in my case)

    # Many to many fields populate          
    for field in hotel_poi._meta.many_to_many:
        if field.name == 'views':
            if u'Vista' in api_poi_review_fields:
                api_vistas = api_poi_review[u'Vista']
                # The api_vistas string comes from api in the format (v1; v2; v3; v4)
                views = api_vistas.split(';')
                choices = field.get_choices()
                temp = []
                for view in views:
                    for choice in choices:
                        if view.lower().strip() == choice[1].lower().strip():
                        temp.append(choice[0])          
                data[field.name]=temp
    

    All of this could be avoided if I had direct database access… In that case i would just need to set an object instance like m = MyModel.objects.filter(id=1) and call form = MyEditForm(instance=m)

    But that was not the case and that’s what makes this question a bit particular.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.