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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:11:13+00:00 2026-05-22T21:11:13+00:00

I have a Django ModelForm in Google App Engine with a ChoiceField, let’s say

  • 0

I have a Django ModelForm in Google App Engine with a ChoiceField, let’s say location:

class MyForm(ModelForm):
    location = ChoiceField(label="Location")

    class Meta:
        model = MyModel

In order to dynamically add the choices for location, and not have issues with app caching, I add them after the form has initialized:

form = MyForm(request.POST, instance=my_instance)
form.fields['location'].choices = Location.all().fetch(1000)

The problem I’m having now is that when the form is initialized via the data in request.POST the choices do not yet exist and I am receiving an error stating that an invalid choice is made (since the value does not yet exist in the list of choices).

I don’t like that validation is occurring when I am initializing the form instead of waiting until I call form.is_valid(). Is there any way to suppress validation during my object instantiation? Or some other way to fix this?

UPDATE: I’m pretty sure ModelFormMetaclass is causing me my grief by validating the provided instance when the form is created. Still not sure how to fix though.

Thanks!

  • 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-22T21:11:14+00:00Added an answer on May 22, 2026 at 9:11 pm

    There must be other ways to do this, but possibly the most straightforward is to add the field in the form’s __init__() method:

    class MyForm(ModelForm):
        ...
        def __init__(self, *args, **kwargs):
            try:
                dynamic_choices = kwargs.pop('dynamic_choices')
            except KeyError:
                dynamic_choices = None # if normal form
            super(MyForm, self).__init__(*args, **kwargs)
            if dynamic_choices is not None:
                self.fields['location'] = ModelChoiceField(
                                              queryset=dynamic_choices)
        class Meta:
            model = MyModel
    

    And your view would look something like:

    def my_view(request):
        locations = Location.objects.all() # or filter(...) or whatever
        dynamic_form = MyForm(dynamic_choices=locations)
    
        return direct_to_template(request, 
                                  'some_page.html',
                                  {'form': dynamic_form},)
    

    Let us know how that works for you.

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

Sidebar

Related Questions

I have a slight issue with dates in Django and Google App Engine :
I am currently using Django forms with the Google App Engine and I have
Let say I have a django ModelForm which I want to edit before saving.
I have a Model and ModelForm like: from django.utils.translation import ugettext_lazy as _ class
I have a sample form: class AdminDiscountForm(ModelForm): class Meta: model = Discount exclude =
I have a ModelForm in my Django app that uses a forms.ModelMultipleChoiceField, which displays
I have a form: class AdForm(ModelForm): class Meta: model = Ad widgets = {
I have built a website based on Google App Engine. It includes ten models,
I have a form in Django that looks like this class FooForm(forms.ModelForm): foo_field =
Newbie question. I have Django models that look like this: class Video(models.Model): uploaded_by =

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.