[Django] #32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'

529 views
Skip to first unread message

Django

unread,
Apr 8, 2021, 3:36:31 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-----------------------------------------+------------------------
Reporter: Seb G | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
= 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:

{{{#!python
class Ticket(models.Model)
user = models.ForeignKey("users.User, on_delete=models.PROTECT,
verbose_name=_("user"))
}}}

And the following admin:

{{{#!python
@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
autocomplete_fields = ("user",)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32619>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 8, 2021, 3:37:02 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Seb G:

Old description:

New description:

= Summary

= Steps to reproduce

Use the following model:

{{{#!python
class Ticket(models.Model)
user = models.ForeignKey("users.User", on_delete=models.PROTECT,
verbose_name=_("user"))
}}}

And the following admin:

{{{#!python
@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
autocomplete_fields = ("user",)
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:1>

Django

unread,
Apr 8, 2021, 3:57:31 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Seb G):

Cannot reproduce with my own base example since a `ForeignKey` instance is
passe to `get_queryset`, as opposed to the `ManyToOneRel` that is passed
in my real-life unexposed example. Investigating.

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:2>

Django

unread,
Apr 8, 2021, 4:15:04 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Seb G):

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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:3>

Django

unread,
Apr 8, 2021, 4:30:19 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => needsinfo


Comment:

`autocomplete_fields` doesn't support reverse M2O relations. Please reopen
the ticket if you can debug your issue and provide a sample project.

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:4>

Django

unread,
Apr 8, 2021, 4:53:09 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Seb G):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:5>

Django

unread,
Apr 8, 2021, 7:50:40 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Seb G):

Problem solved. Cause was traced down to an evolution of the
`BaseModelAdmin.formfield_for_foreignkey` method:

{{{#!python
if db_field.name in self.get_autocomplete_fields(request):
kwargs['widget'] = AutocompleteSelect(db_field.remote_field,
self.admin_site, using=db)
}}}

became

{{{#!python
if db_field.name in self.get_autocomplete_fields(request):
kwargs['widget'] = AutocompleteSelect(db_field, self.admin_site,
using=db)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:6>

Django

unread,
Apr 8, 2021, 11:38:05 AM4/8/21
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* resolution: needsinfo => invalid


Comment:

Thanks for the follow up.

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:7>

Django

unread,
Jan 11, 2022, 7:06:24 AM1/11/22
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Timothy Allen):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:8>

Django

unread,
Mar 5, 2022, 6:22:15 AM3/5/22
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by benedicthsieh):

Replying to [comment:8 Timothy Allen]:


> 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.

I wasn't able to find version 0.8.3 of django-admin-autocomplete-filter,
do you have a source? https://github.com/farhan0581/django-admin-
autocomplete-filter/releases only goes up to 0.7.1, which is still broken.
Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/32619#comment:9>

Django

unread,
Nov 1, 2022, 4:32:49 PM11/1/22
to django-...@googlegroups.com
#32619: 'ManyToOneRel' object has no attribute 'get_limit_choices_to'
-------------------------------+--------------------------------------

Reporter: Seb G | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by cash):

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

Reply all
Reply to author
Forward
0 new messages