Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter the FK list, is it possible? #20

Open
lagaguate opened this issue Apr 2, 2024 · 2 comments
Open

Filter the FK list, is it possible? #20

lagaguate opened this issue Apr 2, 2024 · 2 comments

Comments

@lagaguate
Copy link

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

@elapouya
Copy link
Owner

elapouya commented Apr 4, 2024

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

@lagaguate
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants