Maybe my question is trivial, but I’m new and did not find the answer. Sorry.
And now to the point:
I have a database of servers belonging to some organizations or not belonging to them, and I want to filter the last (servers with emthy field ‘organization’) with dropdown-list filter on my HTML page.
Ок. I make a class with ORM queryset:
class ServerFilter(CustomFilterSet): organization = django_filters.ModelChoiceFilter( to_field_name='organization', label='organization', queryset=Server.objects.order_by('organization').values_list( 'organization', flat=True).distinct(), method='filter_organization__name' ) ...
which absolutely correct reflects my organization list with emthy field:
['', 'org1', 'org2',...]
than, I go to the views and say:
class ServerListView(ExportMixin, CustomPermissionRequiredMixin, SingleTableMixin, FilterView): table_class = ServerTable model = Server template_name = 'mysite/table_default.html' filterset_class = ServerFilter ...
after what I go to the urls and say:
path('servers', views.ServerListView.as_view(), name='list_servers'),
While everything seems like everyone else.
And like everyone else I get that in HTML:
<option value="" selected>Any organization</option> # i used empty_label <option value=""></option> <option value="org1">org1</option> <option value="org2">org2</option>
and see, that second option, reflecting empty field, has an emthy value and indistinguishable from “any” option, and with selecting this option, my filter function, of course, is not even called, and I can not select what I want. That is, the content of my tuple were simply copied to the values of this list…
I even agree to replace this empty value in some way, and then deal with it in the filter function …
But where and what to change?
In what place does this arrangement take place?
Or maybe there is some other way?
I’m sitting dull for the third day and can’t figure it out.
Help me, please.