Friday, September 27, 2024

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

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?

On Fri, 27 Sept 2024 at 22:05, Cương Vũ Thế <vuthecuongjapan@gmail.com> wrote:

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.

--
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+unsubscribe@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

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANVqoJ_6Fq13kV4uAxR80iKmGB_pd2dO152iCZ2TCzzcyh%2BzPg%40mail.gmail.com.

No comments:

Post a Comment