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

Can't use DateRangeFilter on a baseadmin #91

Closed MehdiAria closed 1 year ago

MehdiAria commented 1 year ago

The issue is also explained here: https://stackoverflow.com/questions/74456155/django-error-while-using-daterangefilter-on-a-baseadmin-in-admin-py-file You can't extend the list_filter by adding DateRangeFilter to it

silentsokolov commented 1 year ago

Full stacktrace please

MehdiAria commented 1 year ago
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
      response = get_response(request)
    File "/usr/local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/options.py", line 614, in wrapper
      return self.admin_site.admin_view(view)(*args, **kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
      response = view_func(request, *args, **kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 233, in inner
      return view(request, *args, **kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
      return bound_method(*args, **kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/options.py", line 1693, in changelist_view
      cl = self.get_changelist_instance(request)
    File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/options.py", line 735, in get_changelist_instance
      return ChangeList(
    File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/views/main.py", line 99, in __init__
      self.queryset = self.get_queryset(request)
    File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/views/main.py", line 450, in get_queryset
      ) = self.get_filters(request)
    File "/usr/local/lib/python3.8/site-packages/django/contrib/admin/views/main.py", line 137, in get_filters
      spec = list_filter(request, lookup_params, self.model, self.model_admin)
  TypeError: __init__() missing 2 required positional arguments: 'model_admin' and 'field_path'
silentsokolov commented 1 year ago

Try:

res.append(('created',DateRangeFilter)) 

get_list_filter must return ['in_paid', 'state', ('updated_at', DateTimeRangeFilter)], not ['in_paid', 'state', 'updated_at', DateTimeRangeFilter]

MehdiAria commented 1 year ago

@silentsokolov Now I get this error:

  Exception in thread django-main-thread:
  Traceback (most recent call last):
    File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
      self.run()
    File "/usr/local/lib/python3.8/threading.py", line 870, in run
      self._target(*self._args, **self._kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
      fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
      self.check(display_num_errors=True)
    File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 442, in check
      raise SystemCheckError(msg)
  django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

  ERRORS:
  <class 'myapp.admin.ExampleAdmin'>: (admin.E115) The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.

My code:

from rangefilter.filters import DateRangeFilter

class BaseDateAdmin(admin.ModelAdmin):
    def get_list_filter(self, request):
        res = list(super().get_list_filter(request))
        res.append(('created',DateRangeFilter))
        return list(set(res))

@admin.register(Example)
class ExampleAdmin(BaseDateAdmin):
    list_filter = (
        ('in_paid', 'state'),
    )
silentsokolov commented 1 year ago

Show that get_list_filter return

MehdiAria commented 1 year ago

Show that get_list_filter return

@silentsokolov I used print(list(set(res))) before return but I got no output The only thing I see is the error above

silentsokolov commented 1 year ago

wathever, I know your result is not ['in_paid', 'state', ('updated_at', DateTimeRangeFilter)] whether [('in_paid', 'state'), ('updated_at', DateTimeRangeFilter)] where list_filter[0][1] is state or something else, but not correct result

MehdiAria commented 1 year ago

@silentsokolov Do you know how to solve this issue?

silentsokolov commented 1 year ago
    list_filter = (
        ('in_paid', 'state'),
    )
    list_filter = (
        'in_paid', 'state',
    )
MehdiAria commented 1 year ago

@silentsokolov Still the same error

    list_filter = (
        ('in_paid', 'state'),
    )
silentsokolov commented 1 year ago

correct is

list_filter = (
        'in_paid', 'state',
    )
MehdiAria commented 1 year ago

Solved Thanks