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 form that looks like this: class SampleForm(forms.Form): text = forms.CharField(max_length=100) In
I have a form that looks kind of like this: <div> <div class=contact> <h1>Person's
I have a django model and model form that look like this: -models.py class
Say I have django model that looks something like this: class Order(models.Model): number =
I have a form that looks like this. <form action=index.php method=post> <select name=dropdownOption> <option
I have a simple form that looks like this: <%= simple_form_for @study,:url => studies_path,
I have a page that looks like this: Head and body tags... <form runat=server>
So I have a few Django models that look like this: class City(models.Model): name
I have a simple form that looks like so <% remote_form_for post, :url =>
I have a sub form that looks like a datasheet view. It returns prices

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.