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 filter doesn't work for annotated fields #3

Open
freanuperiaa opened this issue Apr 25, 2019 · 5 comments
Open

the filter doesn't work for annotated fields #3

freanuperiaa opened this issue Apr 25, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@freanuperiaa
Copy link

we're trying to use it for annotated field but it doesn't work, says because the model doesn't have such field. Any workaround?

@freanuperiaa
Copy link
Author

what worked for us is this:

class AnnotatedFieldRangeNumericFilter(admin.SimpleListFilter):
    request = None
    template = 'admin/filter_numeric_range.html'

    def __init__(self, request, params, model, model_admin):
        super().__init__(request, params, model, model_admin)

        self.request = request

        if self.parameter_name + '_from' in params:
            value = params.pop(self.parameter_name + '_from')
            self.used_parameters[self.parameter_name + '_from'] = value

        if self.parameter_name + '_to' in params:
            value = params.pop(self.parameter_name + '_to')
            self.used_parameters[self.parameter_name + '_to'] = value

    def lookups(self, request, model_admin):
        return [self.parameter_name]

    def queryset(self, request, queryset):
        filters = {}

        value_from = self.used_parameters.get(self.parameter_name + '_from', None)
        if value_from is not None and value_from != '':
            filters.update({
                self.parameter_name + '__gte': self.used_parameters.get(self.parameter_name + '_from', None),
            })

        value_to = self.used_parameters.get(self.parameter_name + '_to', None)
        if value_to is not None and value_to != '':
            filters.update({
                self.parameter_name + '__lte': self.used_parameters.get(self.parameter_name + '_to', None),
            })

        return queryset.filter(**filters)

    def expected_parameters(self):
        return [
            '{}_from'.format(self.parameter_name),
            '{}_to'.format(self.parameter_name),
        ]

    def choices(self, changelist):
        return ({
            'request': self.request,
            'parameter_name': self.parameter_name,
            'form': RangeNumericForm(name=self.parameter_name, data={
                self.parameter_name + '_from': self.used_parameters.get(self.parameter_name + '_from', None),
                self.parameter_name + '_to': self.used_parameters.get(self.parameter_name + '_to', None),
            }),
        }, )


class CommentsLast24hRangeNumericFilter(AnnotatedFieldRangeNumericFilter):
    title = 'Comments for last 24 hours'
    parameter_name = 'comments_last_24h'

do you think we can add something like that into the project?

@lukasvinclav
Copy link
Owner

Hi,

do you mean this type of annotations?

https://docs.djangoproject.com/en/2.2/ref/models/querysets/#annotate

@lukasvinclav lukasvinclav added the enhancement New feature or request label Jun 27, 2020
@fabriziofranco
Copy link

@freanuperiaa Is not working: The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.

@felbinger
Copy link

@freanuperiaa @fabriziofranco any update on this issue?

@fabriziofranco
Copy link

https://github.com/proyecto-agricultores/backend/blob/main/agricultores/admin.py I made my own version for annotated fields, but I did not generalize because of deadlines, it is perfectly functional tho.

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

No branches or pull requests

4 participants