Project, which brings a widget, which has the function of loading a select list from another select list.
- This project was made for Django projects.
- The two fields involved must be linked to by forekeys.
- To install you should use the command pip install django_select_by
- In your form.pyfile you must import thedjango_select_bylib and used in the field to be populated, it must be of typeModelChoiceFieldusing theSelectBywidget (select_by = 'bank') passing the parent.
fromt django_select_by.widget import SelectBy
class MyForm(forms.Form):
   
    banco = forms.ModelChoiceField(label='Banco', required=CADASTRAL, widget=forms.Select(),
                                   queryset=Banco.objects.all())
    agencia = forms.ModelChoiceField(label='Agência', required=CADASTRAL, widget=SelectBy(select_by='banco'),
                                     queryset=Agencia.objects.all())
- In your template you must include the file select_by.jsand use in your change method of the parent field theselect_bymethod
{% include 'django_select_by/select_by.html' %}
(function($)
{
    $('#id_banco').change(function()
    {
        var agencia = $('#id_agencia').val();
        if($(this).val() && $(this).val() > 0)
            select_by('id_agencia', $(this).val());
        $('#id_agencia').val(agencia);
    })
    .trigger('change');
})
(jQuery);