1

I am creating a dropdown in forms.py and to filter data I am getting a variable named layer_id . And getting in view like this layer_id = kwargs['layer_id'] . All things are good but when I assign on end to filed like this self.fields['apn'].queryset= Field.objects.filter(layer=layer) . Call goes to fields table of database rather then assigning these fields I declared in forms.py. So who can I assign to fields in forms.py

class labelModelForm(forms.ModelForm):
    model = Labels_tool_
    fields = ['apn', 'owner_name', 'situs_addr_One', 'situs_addr_Two', 'mailing_addr_One', 'mailing_addr_Two']
    apn = forms.ModelChoiceField(queryset=Field.objects.values_list('name', flat=True), empty_label="(Choose field)")

    def __init__(self, *args, **kwargs):
  //getting layer_id from views
        layer_id = kwargs['layer_id']
        print         layer_id
        layer=Layer.objects.filter(id=layer_id).first()
        x=Field.objects.filter(layer=layer)
         //On this step when modify queryset I get error
        self.fields['apn'].queryset= Field.objects.filter(layer=layer)
Learner
  • 238
  • 3
  • 17

2 Answers2

1
def __init__(self, *args, **kwargs):
    layer_id = kwargs.pop('layer_id', None)
    super(labelModelForm, self).__init__(*args, **kwargs)
    layer=Layer.objects.filter(id=layer_id).first()
    x=Field.objects.filter(layer=layer)
    self.fields['apn'].queryset= Field.objects.filter(layer=layer)

i hope this will help you

change your form to

class labelModelForm(forms.ModelForm):
      apn = forms.ModelChoiceField(Field.objects.values_list('name', flat=True), empty_label="(Choose field)")
    class Meta:
        model = Labels_tool_
        fields = ['apn', 'owner_name', 'situs_addr_One', 'situs_addr_Two', 'mailing_addr_One', 'mailing_addr_Two']
Exprator
  • 26,992
  • 6
  • 47
  • 59
  • what you changed ? – Learner Jul 27 '17 at 08:12
  • use it then you will understand – Exprator Jul 27 '17 at 08:12
  • gave error "__init__() got an unexpected keyword argument 'layer_id'" and still not accessing fields in forms – Learner Jul 27 '17 at 08:13
  • Now giving a textfield instead of dropdown. before it nothing was appear. Now how can I get dropdown instead of textbox ? – Learner Jul 27 '17 at 08:18
  • try this, sorry about the early one @Learner – Exprator Jul 27 '17 at 08:22
  • 'name' is field of table 'Field' I only need data of this 'name' field . So I cant edit this – Learner Jul 27 '17 at 08:22
  • You are genius . you are brilliant no doubt. I was stuck from 3 days. How you did this ? what was problem actually ? And last thing from where I can learn these type of advance things on django kindly guide me – Learner Jul 27 '17 at 08:28
  • the problem i didnt notice at first was that you were not providing the Meta class, it is mandatory as you are using modelForm, for advanced stuff you need to go through the functions described in the documentation of django, – Exprator Jul 27 '17 at 08:30
  • why we declared apn field before class ? I saw all work done in class meta mostly . And thank you sir :) – Learner Jul 27 '17 at 08:35
  • hey bro dont call me sir @Learner even i dont know a lot of things. i am also learning, – Exprator Jul 27 '17 at 08:57
0

if you want to some variable to form instance, you can use get_form_kwargs method. you can use it in __init__.

#views.py

class ExampleRegisterView(CreateView):
    model = ExampleModel
    form_class = RegisterExampleForm
    template_name = 'example.html'

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['layer_id'] = 12 # for example, I give 12.
        return kwargs

#forms.py

class RegisterExampleForm(forms.ModelForm):
    apn = forms.ChoiceField(label="test")
    class Meta:
        model = ExampleModel
        fields = ['yourfield', ]
    def __init__(self, *args, **kwargs):
        layer_id = kwargs.pop('layer_id', None)
        super().__init__(*args, **kwargs)
        if layer_id:
            layer = Layer.objects.get(id=layer_id).first()
        self.fields['apn'].choices = Field.objects.filter(layer=layer)
Jayground
  • 1,797
  • 1
  • 17
  • 32