How to change a form dynamically?

Asked

Viewed 37 times

1

I have two models, Empresa and Funcionario. I would like when the user selects the company the employees are dynamically set in the employee form as both Forms will be shown on the same page. So initially on the page I have the form to select the company and immediately after the selection the employee data linked to the company appear to the user.

I made the forms as follows :

class Form_empresa(ModelForm):
    class Meta:
        model = Empresa
        fields = ['nome']


class Form_funcionario(ModelForm):
    class Meta:
        model = Funcionario
        fields = ['nome']

    def __init__(self, *args, **kwargs):
        super(Form_funcionario, self).__init__(*args, **kwargs)
        self.fields['nome'].queryset = Funcionario.objects.filter(empresa = Form_empresa.cleaned_data.get('nome'))

But I get the following mistake :

'Form_empresa' object has no attribute 'get'

It is possible to change one form immediately when the other is changed (on the same page) without doing Submit in Django ?

  • Yes, you make a request with ajax, pass the id of the form and only send the attributes of that form to the server you can read them all with the request.POST , however, you no longer have form.isvalid validation().

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.