Take the 2-minute tour ×

i currently have models that look like this

class Lg(models.Model):
    name = models.CharField(max_length = 50,choices = LG_CHOICES,unique = True)

class State(models.Model):
    name = models.CharField(max_length = 50,choices = STATE_CHOICES,unique = True)
    lg = models.ManyToManyField(Lg,related_name ='State_Lg',blank= True,null = True)

class AddPlaceForm(forms.Form):
    house_number = forms.IntegerField()
    street_name = forms.CharField()
    area = forms.CharField()
    city  = forms.CharField()
    lg = forms.ChoiceField(choices = Lg.objects.none()) 

    def __init__(self,state_id,*args,**kwargs):
        self.fields['lg'] = forms.ChoiceField(choices=list(State.objects.get(id = state_id).lg.select_related()))

I'd like the user to pass in the state id from the view like this form = AddPlaceForm(state_id)

can any one pls help with this

share|improve this question
What exactly are you trying to achieve? –  Raunak Agarwal Oct 28 '12 at 17:04

1 Answer 1

up vote 0 down vote accepted

In your view code:

form = AddPlaceForm(state_id=state_id}


     state_id = kwargs.pop('state_id', None)
     super(AddPlaceForm, self).__init__(*args, **kwargs)
     self.fields['lg'] = forms.ChoiceField(choices = ((lg.id, lg.name) for lg in State.lg.filter(id=state_id))
share|improve this answer
thanks but you have not told me what my lg field will look like –  user1711168 Oct 28 '12 at 18:19
You didn't ask. –  Daniel Roseman Oct 28 '12 at 18:54
@DanielRoseman How will the lg field look like and the form thanks any one else can give a hand too thanks –  user1711168 Oct 30 '12 at 19:47
I edited my answer check it out. Let me know if you find any issues –  Raunak Agarwal Oct 30 '12 at 19:56
@RaunakAgarwal thanks for you help i have not tried to use this yet but with th int come after the other fields or before.Thanks –  user1711168 Nov 5 '12 at 10:02

Your Answer


