elapouya/django-listing

Filter the FK list, is it possible?

Opened this issue · 2 comments

On the filter screen I have several fields that are FK. These call a LOVdetail table that has the list of values for this field. Is it possible to filter the values?

Right now the entire list is shown and that is not correct.

This type of filter worked for me on the form, but when trying with django_listing, I couldn't get it to work.

I share with you how I want to implement it in filter

ForeignKeyFilter(
            "fk_estado",
            order_by="name",
            label="Estado civil",
            format_label=lambda c: f"{c.name} ({c.value})",
            queryset = PteLovd.objects.filter(fk_id_lovh_id__in=PteLovh.objects.filter(codigo='LOV1').values_list('id'))
        ),

I see that in filter, it does not support queryset, how could I solve it?

image

You can create your own filter class by sub-classing ForeignKeyFilter:

class MyForeignKeyFilter(ForeignKeyFilter):
    def get_related_qs(self):
        return PteLovd.objects.filter(fk_id_lovh_id__in=PteLovh.objects.filter(codigo='LOV1')

Note : I did no test

Great, your solution worked great for me, thanks for the help!!