#32619 closed Bug (invalid)
'ManyToOneRel' object has no attribute 'get_limit_choices_to'
Reported by: | Seb G | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Summary
The newly introduced feature in Django 3.2
ModelAdmin.autocomplete_fields now respects ForeignKey.to_field and ForeignKey.limit_choices_to when searching a related model)
Triggers a bug when used on ManyToOneRel
:
Traceback (most recent call last): File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 250, in wrapper return self.admin_view(view, cacheable)(*args, **kwargs) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/utils/decorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 232, in inner return view(request, *args, **kwargs) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/contrib/admin/sites.py", line 417, in autocomplete_view return AutocompleteJsonView.as_view(admin_site=self)(request) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/views/generic/base.py", line 98, in dispatch return handler(request, *args, **kwargs) File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/contrib/admin/views/autocomplete.py", line 25, in get self.object_list = self.get_queryset() File "/home/me/.virtualenvs/myproject/lib/python3.7/site-packages/django/contrib/admin/views/autocomplete.py", line 42, in get_queryset qs = qs.complex_filter(self.source_field.get_limit_choices_to()) AttributeError: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
Steps to reproduce
Use the following model:
class Ticket(models.Model) user = models.ForeignKey("users.User", on_delete=models.PROTECT, verbose_name=_("user"))
And the following admin:
@admin.register(Ticket) class TicketAdmin(admin.ModelAdmin): autocomplete_fields = ("user",)
Change History (9)
comment:1 by , 4 years ago
Description: | modified (diff) |
---|
comment:2 by , 4 years ago
comment:3 by , 4 years ago
For some reason, in my base example, the data-
attributes on the HTML select
are as follow:
data-app-label="myapp"
data-model-name="ticket"
data-field-name="user"
While in my real-life project they seem to be reversed as follow:
data-app-label="users"
data-model-name="User"
data-field-name="ticket"
This causes a ManyToOneRel
to be passed to AutocompleteJsonView.get_queryset
instead of the expected ForeignKey
.
comment:4 by , 4 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
autocomplete_fields
doesn't support reverse M2O relations. Please reopen the ticket if you can debug your issue and provide a sample project.
comment:5 by , 4 years ago
The fact is that i do have an autocomplete field that was working perfectly in 3.1 and fails to work in 3.2, due to this new feature. I will keep trying to replicate and provide a sample project.
comment:6 by , 4 years ago
Problem solved. Cause was traced down to an evolution of the BaseModelAdmin.formfield_for_foreignkey
method:
if db_field.name in self.get_autocomplete_fields(request): kwargs['widget'] = AutocompleteSelect(db_field.remote_field, self.admin_site, using=db)
became
if db_field.name in self.get_autocomplete_fields(request): kwargs['widget'] = AutocompleteSelect(db_field, self.admin_site, using=db)
and this broke an override from a package we use.
comment:8 by , 3 years ago
For anyone who finds this ticket with the same error: this error will be triggered if you are using the third party Django admin packages django-admin-autocomplete-filter
or django-admin-rangefilter
. Upgrade to the latest versions (as of this writing, django-admin-rangefilter==0.6.3
and django-admin-autocomplete-filter==0.8.3
) and it should resolve the error.
comment:9 by , 2 years ago
For those confused by the previous comment, there is no django-admin-autocomplete-filter version 0.8.3 and the newest version is 0.7.1 which is not compatible with Django 3.2 or greater. Link to the github repo for that package: https://github.com/farhan0581/django-admin-autocomplete-filter
Cannot reproduce with my own base example since a
ForeignKey
instance is passe toget_queryset
, as opposed to theManyToOneRel
that is passed in my real-life unexposed example. Investigating.