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

  • Home
  • SEARCH
  • 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 4065622
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:00:24+00:00 2026-05-20T16:00:24+00:00

I am trying to create a form in that ModelChoiceField loads from queryset and

  • 0

I am trying to create a form in that ModelChoiceField loads from queryset and i want add few custom values to ModelChoiceField for extend i have used choice field, like below but while updating the form,getting below error

Form Error :
Select a valid choice. That choice is not one of the available choices.

Code :

 self.fields['lead'] = forms.ModelChoiceField(queryset = Pepole.objects.filter(poc__in       = ('lead','sr.lead')))
 self.fields['lead2'] = forms.ModelChoiceField(queryset = Pepole.objects.filter(role__in = ('lead','sr.lead')))
      choice_field = self.fields['lead']                                    
      choice_field.choices = list(choice_field.choices) + [('None', 'None')]
      choice_field = self.fields['lead2']                                    
      choice_field.choices = list(choice_field.choices) + [('None', 'None')]

Am i doing any thing wrong here?

  • 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-20T16:00:25+00:00Added an answer on May 20, 2026 at 4:00 pm

    That’s not going to work. Look at how a ModelChoiceField works:

    try:
        key = self.to_field_name or 'pk'
        value = self.queryset.get(**{key: value})
    except self.queryset.model.DoesNotExist:
        raise ValidationError(self.error_messages['invalid_choice'])
    return value
    

    You can’t add something randomly to it.

    Use a ChoiceField instead and custom process the data.

    class TestForm(forms.Form):
        mychoicefield = forms.ChoiceField(choices=QS_CHOICES)
    
        def __init__(self, *args, **kwargs):
            super(TestForm, self).__init__(*args, **kwargs)
            self.fields['mychoicefield'].choices = \
                list(self.fields['mychoicefield'].choices) + [('new stuff', 'new')]
    
        def clean_mychoicefield(self):
            data = self.cleaned_data.get('mychoicefield')
            if data in QS_CHOICES:
                try:
                    data = MyModel.objects.get(id=data)
                except MyModel.DoesNotExist:
                    raise forms.ValidationError('foo')
            return data
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a form that has an expandable widget. The problem is
I am trying to create a form in Rails 2.2.2 that populates a field
I'm trying to create a form that will animate something while processing a particular
I am trying to create a form that posts data via jQuery and populates
I'm trying to create a form that based on the users input would determine
Hello there i'm trying to create a form with Zend_Form that will enable my
I'm trying to create an input form similar to a text message. I have
I've been trying to create my own custom LiveValidation ( LiveValidation.com ) function that
I'm trying to create a form that will show the id that the record
I'm trying to create a form that sends the customer's name and email address

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.