farhan0581 / django-admin-autocomplete-filter

A simple Django app to render list filters in django admin using autocomplete widget.
GNU General Public License v3.0
351 stars 76 forks source link

Allow related field to be used in filter #30

Closed Jerome-Celle closed 4 years ago

Jerome-Celle commented 4 years ago

This PR can solve: #27 , #11

This change allow filtering on related field like artiste__record_label, with a Record Label model. I added parameter_name in attribute, so we can set his value before, and it seems enough to allow the filter on related field.

Example: Filter order line with the user that is present on the order

class OrderUserFilter(CustomAutocompleteFilter):
    title = 'User'
    field_name = 'user'
    rel_model = Order
    parameter_name = 'order__user'

class OrderLineAdmin(admin.ModelAdmin):
    list_filter = (
        OrderUserFilter,
    )
Jerome-Celle commented 4 years ago

Hi @farhan0581 or @farhan-cashify can you check my PR? :)

seocam commented 4 years ago

That would be extremely helpful.

For now I'm using an ugly hack that might be help others while the feature is not released.


class OrderUserFilter(AutocompleteFilter):
    title = 'User'
    field_name = 'user'
    rel_model = Order

    @property                                                                                
    def parameter_name(self):                                                                
        return "order__user"                                                     

    @parameter_name.setter                                                                   
    def parameter_name(self, value):                                                         
        pass       

class OrderLineAdmin(admin.ModelAdmin):
    list_filter = (
        OrderUserFilter,
    )
    def lookup_allowed(self, lookup, value):                                                 
        if lookup == "order__user":                                              
            return True                                                                      
        return super().lookup_allowed(lookup, value) 
edgarsj commented 4 years ago

That would be extremely helpful.

Agreed. @seocam, thanks, you just saved me a working day looking for a solution.

alcad commented 4 years ago

sure @seocam this is a really needed feature

filippo-20tab commented 4 years ago

I really need this too! Thanks.

farhan0581 commented 4 years ago

sorry guys, was a little occupied, will work on this, thanks for contribution !

farhan0581 commented 4 years ago

Checkout the latest pypi version 0.5