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

The selected filter does not filter the model in the admin panel #1353

Open
SiberMix opened this issue Feb 4, 2024 · 3 comments
Open

The selected filter does not filter the model in the admin panel #1353

SiberMix opened this issue Feb 4, 2024 · 3 comments

Comments

@SiberMix
Copy link

SiberMix commented Feb 4, 2024

The selected filter does not filter the model in the admin panel

I migrated DAL==3.5.1 for django 4 (replace ugettext to gettext)
I did this because the dropdown with the autocomplete did not work and there was ----
Now I select an entry from the dropdown, but the model in the admin panel is not filtered - what could be the matter?

class OfferFilter(AutocompleteFilter):
    title = _('Оффер')
    field_name = 'name'
    autocomplete_url = 'admin:offer-search-select'


    
    def get_queryset_for_field(self, model, name):
        return Offer.objects.all()

    def queryset(self, request, queryset):
        if self.value() and self.value().isdigit():
            return queryset.filter(id=int(self.value()))
        return queryset
@admin.register(Offer)
class OfferAdmin(ExportMixinAdmin, ArchiveMixin, TabbedTranslationAdmin, NestedModelAdmin, AdminConfirmMixin):
    inlines = [OfferSourceTypeNoteInline, OfferPageInline, OfferTargetInline, OfferContestConfigInline]
    filter_horizontal = ['countries', 'custom_webmasters', 'offer_source_types']
    list_display = (
        'id', 'name', 'advertiser_link', 'offer_manager', 'publish_dt', 'offer_type', 'is_hidden', 'status',
        'last_start', 'stat_refresh_info', 'integration_method', 'is_sponsor_display', 'get_sponsor_position'
    )
    search_fields = ['name', 'search_options', '=id']

    list_filter = (
        OfferFilter, )

Please help me.

I am migrating a very large project from django version 1.9 to >4

I want to cry

@paulocoutinhox
Copy link

Did you solved it?

Im trying make it work with list_filter but without success until now.

@SiberMix
Copy link
Author

@paulocoutinhox I have solved this

It's time to write your own class derived from Autocomplet Filter From the admin_auto_filters library

class SelfAutocompleteFilter(AutocompleteFilter):

    def __init__(self, request, params, model, model_admin):
        if self.parameter_name is None:
            self.parameter_name = self.field_name
            if self.use_pk_exact:
                self.parameter_name += '__{}__exact'.format(self.field_pk)
        super(AutocompleteFilter, self).__init__(request, params, model, model_admin)

        if self.rel_model:
            model = self.rel_model
        remote_field = model._meta.get_field(self.field_name)
        if not isinstance(remote_field, ForeignKey) and not isinstance(remote_field, ManyToManyField):
            remote_field = model._meta.get_field('id')

        widget = AutocompleteSelect(remote_field,
                                    model_admin.admin_site,
                                    custom_url=self.get_autocomplete_url(request, model_admin), )
        form_field = self.get_form_field()
        field = form_field(
            queryset=self.get_queryset_for_field(model, self.field_name),
            widget=widget,
            required=False,
        )
        self._add_media(model_admin, widget)

        attrs = self.widget_attrs.copy()
        attrs['id'] = 'id-%s-dal-filter' % self.parameter_name
        if self.is_placeholder_title:
            attrs['data-Placeholder'] = self.title
        self.rendered_widget = field.widget.render(
            name=self.parameter_name,
            value=self.used_parameters.get(self.parameter_name, ''),
            attrs=attrs
        )

class OfferFilter(SelfAutocompleteFilter):
    title = _('Оффер')
    field_name = 'name'
    rel_model = Offer

    @staticmethod
    def get_queryset_for_field(model, name):
        return Offer.objects.all()


    def queryset(self, request, queryset):
        if self.value() and self.value().isdigit():
            return queryset.filter(id=int(self.value()))
        return queryset

    def get_autocomplete_url(self, request, model_admin):
        return reverse('admin:offer-search')
list_filter = (
        OfferFilter,)

@paulocoutinhox
Copy link

Hi,

Im already using admin_auto_filters. It is working with that.

I think that i can solve only with django-autocomplete-light library.

Thanks.

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