Sunday, March 14, 2010

Re: limit_choices_to a value on a field in the same model

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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate