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

Multiple uses of DateTimeRangeFilter in the same admin.py fail #98

Closed pmi123 closed 1 year ago

pmi123 commented 1 year ago

I have multiple ModelAdmin classes for several models in one admin.py file. Each model has the following two fields:,

    created = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="date created")
    updated = models.DateTimeField(auto_now=True, editable=False, verbose_name="last update") 

and each ModelAdmin class has the fields created and updated in their respective list_filter lines.

I can add the following to one ModelAdmin class for one model:

list_filter = ('document_id__documentType_id', ('created', DateTimeRangeFilter), ('updated', DateTimeRangeFilter), 'task_status', 'task_name',)

But if I try to add the DateTimeRangeFilter to another ModelAdmin for another model, I get this error

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/core/management/base.py", line 475, in check
    all_issues = checks.run_checks(
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/contrib/admin/checks.py", line 51, in check_admin_app
    errors.extend(site.check(app_configs))
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 97, in check
    errors.extend(modeladmin.check())
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/contrib/admin/options.py", line 139, in check
    return self.checks_class().check(self, **kwargs)
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/contrib/admin/checks.py", line 797, in check
    *self._check_list_display(admin_obj),
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/contrib/admin/checks.py", line 887, in _check_list_display
    return list(
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/contrib/admin/checks.py", line 889, in <genexpr>
    self._check_list_display_item(obj, item, "list_display[%d]" % index)
  File "/home/mark/.virtualenvs/archive/lib/python3.8/site-packages/django/contrib/admin/checks.py", line 897, in _check_list_display_item
    elif hasattr(obj, item):
TypeError: hasattr(): attribute name must be string

It gets worse....

In my Django 4.1 project I have multiple apps. Most of all the models in each app have the same two fields for created and updated. When I try to add the DateTimeRangeFilter to a different app's ModelAdmin class for that model's created and updated fields, I get the same error.

Any suggestions on how to fix this?

pmi123 commented 1 year ago

Never mind....operator error. I was putting the DateTimeRangeFilter in the list_display by mistake (also has the created and updated fields). When I added the DateTimeRangeFilters to the list_filter for each model's ModelAdmin, it all worked as advertised.