bugbytes-io / django-htmx-chained-dropdown

Using Django and HTMX to implement a chained/dependent dropdown
16 stars 11 forks source link

Form value method is not bringing value from the form #1

Closed ghulammasood closed 2 years ago

ghulammasood commented 2 years ago

Hey There,

I have added two modelchoicefields on my model by following your blog. My Forms.py File Looks like this

Forms.py

from dynamic_forms import DynamicField, DynamicFormMixin

class WorkOrderForm(DynamicFormMixin, forms.ModelForm): def city_choices(form): to_cities = [] customer = form['customer'].value() contract_rates = ContractRates.objects.filter(customer=customer) for rate in contract_rates: to_cities.append(rate.city_to.id) return City.objects.filter(pk__in=to_cities)

customer = forms.ModelChoiceField(queryset=Customer.objects.all(), required=False)
to_city = DynamicField(forms.ModelChoiceField,
                       required=False,
                       queryset=city_choices)

html_form: ... {% load widget_tweaks %} ...

{% csrf_token %} {{ form.non_field_errors }}
{{ form.customer.errors }}
{% render_field form.customer class='' autocomplete="off" %}
{{ form.from_city.errors }}
{{ form.from_city }}
{{ form.to_city.errors }}
{% render_field form.to_city class='' %}

Everything goes on smooth apart from the fact that the value for the selected customer is not fetched into the city_choices method. Let me know if you can see whats missing or where i could have gone wrong with it.

ghulammasood commented 2 years ago

I was able to figure it out, I had not added the htmx to the base template.