farhan0581 / django-admin-autocomplete-filter

A simple Django app to render list filters in django admin using autocomplete widget.
GNU General Public License v3.0
351 stars 75 forks source link

Not working for OneToOneField #89

Open FinnGu opened 1 year ago

FinnGu commented 1 year ago

Hey :)

I have a OneToOneField defined on model Organisation that points to model AccountingInformation. Now, I would like to filter the admin list view of AccountingInformation via the reverse side of this relation, but it fails.

# models.py
class Organisation(models.Model):
    accounting_information = models.OneToOneField(
        to='accounting.AccountingInformation',
        related_name='organisation'
    )

# admin.py
@admin.register(AccountingInformation)
class AccountingInformationAdmin(admin.ModelAdmin):
    ...
    list_filter = (AutocompleteFilterFactory(title='Organisation', base_parameter_name='organisation'))
    ...

This however results in the following error:

ERROR 2023-02-08 11:48:06,353 Internal Server Error: /admin/autocomplete/
2023-02-08T11:48:06.353755387Z Traceback (most recent call last):
2023-02-08T11:48:06.353757220Z   File "/opt/venv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
2023-02-08T11:48:06.353758887Z     response = get_response(request)
2023-02-08T11:48:06.353760387Z   File "/opt/venv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
2023-02-08T11:48:06.353761970Z     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2023-02-08T11:48:06.353763720Z   File "/opt/venv/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 250, in wrapper
2023-02-08T11:48:06.353765387Z     return self.admin_view(view, cacheable)(*args, **kwargs)
2023-02-08T11:48:06.353766970Z   File "/opt/venv/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
2023-02-08T11:48:06.353768678Z     response = view_func(request, *args, **kwargs)
2023-02-08T11:48:06.353770220Z   File "/opt/venv/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
2023-02-08T11:48:06.353771762Z     response = view_func(request, *args, **kwargs)
2023-02-08T11:48:06.353773345Z   File "/opt/venv/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 232, in inner
2023-02-08T11:48:06.353775012Z     return view(request, *args, **kwargs)
2023-02-08T11:48:06.353776387Z   File "/opt/venv/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 417, in autocomplete_view
2023-02-08T11:48:06.353777887Z     return AutocompleteJsonView.as_view(admin_site=self)(request)
2023-02-08T11:48:06.353779512Z   File "/opt/venv/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
2023-02-08T11:48:06.353781178Z     return self.dispatch(request, *args, **kwargs)
2023-02-08T11:48:06.353782553Z   File "/opt/venv/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
2023-02-08T11:48:06.353784178Z     return handler(request, *args, **kwargs)
2023-02-08T11:48:06.353785720Z   File "/opt/venv/lib/python3.9/site-packages/django/contrib/admin/views/autocomplete.py", line 25, in get
2023-02-08T11:48:06.353787387Z     self.object_list = self.get_queryset()
2023-02-08T11:48:06.353792262Z   File "/opt/venv/lib/python3.9/site-packages/django/contrib/admin/views/autocomplete.py", line 42, in get_queryset
2023-02-08T11:48:06.353794053Z     qs = qs.complex_filter(self.source_field.get_limit_choices_to())
2023-02-08T11:48:06.353795637Z AttributeError: 'OneToOneRel' object has no attribute 'get_limit_choices_to'

Thank you for your help, Finn