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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:59:02+00:00 2026-05-19T13:59:02+00:00

I have a form in my forms.py that looks like this: from django import

  • 0

I have a form in my forms.py that looks like this:

from django import forms

class ItemList(forms.Form):
     item_list = forms.ChoiceField()

I need to populate the item_list with some data from the database. When generated in HTML item_list should be something like:

<select title="ItemList">
   <option value="1">Select Item 1</option>
   <option value="2">Select Item 2</option>
</select>

The options values in my select statement will change almost every time since a variable in the query will often change generating new results.

What do I need to put in the view.py and also in my template files to populate the ItemList with values from the database?

  • 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-05-19T13:59:02+00:00Added an answer on May 19, 2026 at 1:59 pm

    Take a look at this example in the Django documentation:

    • http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#a-full-example

    Basically, you can use the queryset keyword argument on a Field object, to grab rows from your database:

    class BookForm(forms.Form):
        authors = forms.ModelMultipleChoiceField(queryset=Author.objects.all())
    

    Update

    If you need a dynamic model choice field, you can hand over your item id in the constructor of the form and adjust the queryset accordingly:

    class ItemForm(forms.Form):
    
        # here we use a dummy `queryset`, because ModelChoiceField
        # requires some queryset
        item_field = forms.ModelChoiceField(queryset=Item.objects.none())
    
        def __init__(self, item_id):
            super(ItemForm, self).__init__()
            self.fields['item_field'].queryset = Item.objects.filter(id=item_id)
    

    P.S. I haven’t tested this code and I’m not sure about your exact setup, but I hope the main idea comes across.

    Resources:

    • http://www.mail-archive.com/django-users@googlegroups.com/msg48058.html
    • http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ModelChoiceField
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a win form UI that looks like a typical calculator. Naturally I
I have a web report that uses a Django form (new forms) for fields
When I define a Django form class similar to this: def class MyForm(forms.Form): check
I have a dict of configuration variables that looks something like this: self.config =
I have a form that sits behind ASP.NET forms authentication. So far, the implementation
Using web forms I know that you can only have one ASP.NET form on
The basics: I have a contact form that uses php to validate the forms.
In .NET, Windows Forms have an event that fires before the Form is loaded
I have a form that I would like to style. specifcally I would like
I have an array which looks like this $dataArray = array ( 0 =>

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.