Walt, you're a lifesaver!

Many thanks for the great example. I only made a couple of minor
tweeks:

class AgencyForm(forms.ModelForm):
    class Meta:
        model = Agency

    def __init__(self, *args, **kwargs):
        super(AgencyForm, self).__init__(*args, **kwargs)
        if self.instance.id:
            if self.instance.state:
                counties =
County.objects.filter(us_state=self.instance.state)
                county_field = self.fields['counties'].widget
                county_choices = []
                if counties is None:
                    county_choices.append(('', '---------'))
                for county in counties:
                    county_choices.append((county.id, county.name))
                county_field.choices = county_choices

Kind regards,
Brandon

On Mar 14, 8:23 am, Walt <tufelkin...@gmail.com> wrote:
> Here is an idea of what your forms.py should contain:
>
> class AgencyForm(ModelForm):
>
>     class Meta:
>         model = Agency
>
>     def __init__(self, *args, **kwargs):
>         super(AgencyForm, self).__init__(*args, **kwargs)
>         if self.instance.id:
>             if self.instance.state_id:
>                 counties =
> County.objects.filter(state=self.instance.state_id)
>
>                 county_field = self.fields['counties'].widget
>                 county_choices = []
>                 county_choices.append(('', '------'))
>                 for county in counties:
>                     county_choices.append((county.id, county.name))
>                     county_field.choices = county_choices
>
> Then in admin.py, make sure you have:
>
> class AgencyAdmin(admin.ModelAdmin):
>     form = AgencyForm
>
> Hope this helps,
> Walt
>
> -~

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to