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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:54:23+00:00 2026-06-17T16:54:23+00:00

I want to offer form fields based on model choices. So, if in my

  • 0

I want to offer form fields based on model choices. So, if in my models:

COLORS = (
    ('BLA', 'Black'),
    ('WHI', 'White'),
    ('RED', 'Red'),
    ('BLU', 'Blue'),
)

YEAR_CHOICES = []
for r in range(1980, (datetime.datetime.now().year+1)):
    YEAR_CHOICES.append((r,r))

class Thing(models.Model):
    stuff = models.ForeignKey(Stuff)
    color = models.CharField(max_length=3, choices=COLORS)
    year = models.IntegerField(('year'), max_length=4, choices=YEAR_CHOICES, default=datetime.datetime.now().year)

I want to create two form fields, one whose <select> options are YEAR_CHOICES and one whose options are COLORS. Can you do this using ModelChoiceField? Re-defining choices in my forms.py does not seem very DRY.. Otherwise, how would you solve this issue?

Something like this is what I had envisioned, but it does not work:

class BrowseForm(forms.Form):
    stuff = forms.ModelChoiceField(queryset=Stuff.objects.all())
    color = forms.ModelChoiceField(queryset=Thing.color.choices())
    year = forms.ModelChoiceField(queryset=Thing.year.choices())
  • 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-17T16:54:24+00:00Added an answer on June 17, 2026 at 4:54 pm

    from the docs (here)

    you can use the select widget and specify choices. In your case

    color = forms.CharField(max_length=20, widget=forms.Select(choices=COLORS))

    However, since you’ve already defined color with choices you in fact do not need to do anything (other than set the meta of BrowseForm to Thing) to get this to work. It will default to the behavior you want.

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

Sidebar

Related Questions

I want to write hidden inputs to the form based on which checkboxes are
I want to group several standard form fields into a single custom form field
I want to offer audio files for download on my site, after a user
I want to offer the user a list of urls that he/she has clicked
When a user registers with my site I want to offer them a login
A client wants to use Google Apps for employees but they want to offer
This is more like a good practise question. I want to offer different generic
I want to host a web application on a private JVM they offer 32,
I would like to know what I can offer to clients when they want
I am creating a c++ program, but I want to be able to offer

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.