silentsokolov / django-admin-rangefilter

A Django app that lets you filter data by date range and numeric range in the admin UI
MIT License
713 stars 106 forks source link

Filter for calculated fields #86

Closed KaktusOnFire closed 2 years ago

KaktusOnFire commented 2 years ago

Any solution to make custom filter for calculated Admin fields?

Example:

@admin.register(Test)
class TestAdmin(model.ModelAdmin):
    list_display = ("sale_date", "another_field")
    list_filter = (
        ....
    )

    def sale_date(self, obj):     #Field for filter
        result: datetime = do_some_logic()
        return result
silentsokolov commented 2 years ago

Hmm ... I don't know a good solution, but if a query speed does not matter. You can create manager like

class FooBarManager(models.Manager):
    def sale_date(self, start_dt, end_dt):
       do_some_logic()
       return queryset

And then overwrite method queryset.