In django template cannot get data from values_list() for select options dropdown

27 views
Skip to first unread message

Cương Vũ Thế

unread,
Sep 27, 2024, 10:06:13 AMSep 27
to Django users

Im django noob here so I would like to get your support. Thank you so much in advanced. 

1. My model.py:

class Province(models.Model): province_name = models.CharField() class Employee(models.Model): first_name = models.CharField() province = models.ForeignKey(Province, on_delete=models.CASCADE,)

2. My filters.py (django-filter)

from django_filters import FilterSet, filters from .models import ( Employee, Province, ) class EmployeeFilter(FilterSet): province_name_multi_select = ( filters.ModelMultipleChoiceFilter( field_name="province_id", queryset=Province.objects.values_list("id", "province_name"), ) ) class Meta: model = Employee fields = [ "id", "first_name", "province", ]

3. My views.py

class EmployeeListView(FilterView): model = Employee queryset = Employee.objects.select_related( "province", ) form_class = EmployeeForm strict = False context_object_name = "employees_list" filterset_class = EmployeeFilter

** 4. My employee_filter.html**

<select class="form-select" name="province_name_multi_select" id="multiple-select-field" multiple\> {% for element in filter.form.province_name_multi_select %} {{ element.tag }} \<= **Problem happened here** {% endfor %} </select\>

** 5. My problem** Above {{ element.tag }} produced <option value="[1], 'abc province']" </option> But what I want it to produce is, you can guess that <option value="1" </option>.

 Once again, thank you.

Abdul Qoyyuum Haji Abdul Kadir

unread,
Sep 27, 2024, 11:42:07 AMSep 27
to django...@googlegroups.com
Maybe change this:
Province.objects.values_list("id", "province_name")

to this:
Province.objects.values_list("id")

Although that doesn't explain the [] around 1. Are there multiple ids per province_name?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/30be874a-7aad-4fa0-b21f-78b000c7f963n%40googlegroups.com.


--
Abdul Qoyyuum Bin Haji Abdul Kadir
Nickname: Q
Reply all
Reply to author
Forward
0 new messages