views:

1373

answers:

1

I've been trying to figure this out for hours across a couple of days, and can not get it to work. I've been everywhere. I'll continue trying to figure it out, but was hoping for a quicker solution. I'm using App Engine datastore + Django.

Using a query in a view and custom forms, I was able to get a list to the form but then I was not able to post. I have been trying to figure out how to dynamically add the choices as part of the Django form... I've tried various ways with no success. Help!

Below are the two models. I'd like to get a distinct list of address_id to show in the location field in InfoForm. This fields could (and maybe should) be named the same, but I thought it'd be easier if they were named different.

class Info(db.Model):
    user = db.UserProperty()
    location = db.StringProperty()
    info = db.StringProperty()
    created = db.DateTimeProperty(auto_now_add=True)
    modified = db.DateTimeProperty(auto_now=True)

class Locations(db.Model):
    user = db.UserProperty()
    address_id = db.StringProperty()
    address = db.StringProperty()

class InfoForm(djangoforms.ModelForm):
    info = forms.ChoiceField(choices=INFO_CHOICES)
    location = forms.ChoiceField()
    class Meta:
            model = Info
            exclude = ['user','created','modified']
+1  A: 

I'm not familiar with App Engine Datastore, but I'm guessing you probably want to do something along these lines:

class InfoForm(djangoforms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(InfoForm, self).__init__(*args, **kwargs)
        choices = [(r.id, r.info) for r in Info.objects.filter(...)]
        self.fields['info'] = ChoiceField(choices=choices)

By assigning info dynamically, you would then exclude this line from your InfoForm class:

    info = forms.ChoiceField(choices=INFO_CHOICES)
Jeff Bauer