django-admin-list-filter icon indicating copy to clipboard operation
django-admin-list-filter copied to clipboard

Feature request - span foreign key relationships

Open lordp opened this issue 1 year ago • 7 comments

I would love it if you could add the ability to span relationships - please see the same issue report in the predecessor of this package (django-admin-autocomplete-list-filter) for info

https://github.com/demiroren-teknoloji/django-admin-autocomplete-list-filter/issues/2#issuecomment-596537208

lordp avatar Aug 15 '24 09:08 lordp

hmm... dalf uses built-in list filters by default. like regular;

list_filter = ['foo__bar__baz']

should also work. i'll check on it. keep you posted.

vigo avatar Aug 18 '24 11:08 vigo

ok, i've seen the problem :)

vigo avatar Aug 18 '24 11:08 vigo

the problem is related to builtin ajax autocomplete view. only completes foreign keys. i'll investigate how to implement this feature. i don't want to override django's builtin views. i'll see what i can do.

vigo avatar Aug 18 '24 12:08 vigo

list_filter = [('foo_bar_baz', DALFRelatedField)]

works. only DALFRelatedFieldAjax doesn't work due to built-in auto complete view.

vigo avatar Aug 18 '24 12:08 vigo

Yeah, that's the same point I got to. I will see if I can figure out how to change it myself - thanks.

lordp avatar Aug 18 '24 21:08 lordp

i want to implement it, seems a bit challenging. my main goal was to use all the built-ins shipped with django and kind of wrap around w/o adding or tweaking anything as less as much i can. i'll try to to implement a class which inherits from existing AutocompleView and all i need is to intercept queryset. builtin autocomplete view checks the field's existence. in foo_bar_baz there is no field in the model. view designed to work with FK and M2M fields. I'll try to implement it :) Maybe someone help me out? all PR's are welcome :)

vigo avatar Aug 19 '24 06:08 vigo

I found this Stackoverflow answer that may be a good starting point. It's a couple of years old though.

https://stackoverflow.com/questions/70802751/django-admin-autocomplete-field-without-foreignkey-or-manytomany-relationshi

lordp avatar Aug 19 '24 06:08 lordp

I'm using the following workaround and have not encountered any issues yet.

class FixedDALFRelatedFieldAjax(DALFRelatedFieldAjax):
    """
    Fixes the issue that the build-in AutocompleteJsonView cannot handle nested relations.

    Example:
        Incoming parameters:
            field_path = "item__product__supplier"
            field.model = Product
            model = Item
        Original DALFRelatedFieldAjax:
            model_name = "Product"
            field_name = "item__product__supplier"
            Will fail because there is no field "item__product__supplier" on model Product
        FixedDALFRelatedFieldAjax:
            model_name = "Product"
            field_name = "supplier"
    """

    def __init__(self, field, request, params, model, model_admin, field_path):
        super().__init__(field, request, params, model, model_admin, field_path)  # type: ignore

        self.custom_template_params = {
            **self.custom_template_params,
            "app_label": field.model._meta.app_label,
            "model_name": field.model._meta.model_name,
            "field_name": field_path.split("__")[-1],
        }

penenkel avatar Sep 23 '25 15:09 penenkel