Не работает multi Chained DropDown привязка
Использую пакет django-dynamic-forms
Есть views и forms, там 3 формы первая с второй связана и если поменять первую то вторая сбрасыавется и показывает новые значения, но когда сбрасывается вторая или менять значение второй у третьей ничего не меняется или значения вовсе пропадают
Кто-то может помочь с данным решением?
views.py
@login_required
def create_work_log(request):
if request.method == 'POST':
form = WorkLogForm(request.POST, user=request.user)
if form.is_valid():
work_log = form.save(commit=False)
work_log.author = request.user
work_log = form.save()
messages.success(request, 'Данные занесены успешно', {'work_log': work_log})
return redirect('create_worklog')
else:
messages.error(request, 'Ошибка валидации')
return redirect('create_worklog')
else:
if request.user.is_authenticated:
initial = {
'author': request.user
}
else:
initial = {}
form = WorkLogForm(user=request.user, initial=initial)
return render(request, 'contractor/create_work_log.html', {'form': form})
def contractor_object(request):
form = WorkLogForm(request.GET, user=request.user)
return HttpResponse(form['contractor_object'])
def contractor_section(request):
form = WorkLogForm(request.GET, user=request.user)
return HttpResponse(form['contractor_section'])
forms.py
class WorkLogForm(DynamicFormMixin, forms.ModelForm):
def object_choice(form):
counter = form['contractor_counter'].value()
query = ObjectList.objects.filter(contractor_guid__in=[counter])
return query
def object_initial(form):
contractor_counter = form['contractor_counter'].value()
query = ObjectList.objects.filter(contractor_guid__in=[contractor_counter])
return query.first()
def section_choices(form):
contractor_object = form['contractor_object'].value()
section_query = SectionList.objects.filter(object=contractor_object)
return section_query
def section_initial(form):
contractor_object = form['contractor_object'].value()
section_query = SectionList.objects.filter(object=contractor_object)
return section_query.first()
worklog_date = forms.DateField(label='Дата', widget=forms.DateInput(
attrs={'type': 'date', 'class': 'form-control', 'placeholder': 'Введите дату'}))
description = forms.CharField(label='Описание',
widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Описание'}))
contractor_counter = forms.ModelChoiceField(
label='Контрагент',
queryset=CounterParty.objects.none(),
initial=CounterParty.objects.first(),
empty_label='',
)
contractor_object = DynamicField(
forms.ModelChoiceField,
queryset=object_choice,
initial=object_initial,
)
contractor_section = DynamicField(
forms.ModelMultipleChoiceField,
queryset=section_choices,
initial=section_initial,
)
worklog.html
<div class="mt-3">
{{ form.contractor_counter.label_tag }}
{% render_field form.contractor_counter class='form-select mt-2' autocomplete='off' hx-get='/objects/' hx-trigger='change' hx-target='#id_contractor_object' hx-trigger='change'%}
</div>
<div class="mt-3">
{{ form.contractor_object.label_tag }}
{% render_field form.contractor_object class='form-select mt-2' autocomplete='off' hx-get='/sections/' hx-trigger='change' hx-target='#id_contractor_section' %}
</div>
<div class="mt-3">
{{ form.contractor_section.label_tag }}
{% render_field form.contractor_section class='form-select mt-2' autocomplete='off' %}
</div>