[Django] #29138: Admin autocomplete field doesn't support the 'to_field' on Foreign Keys

51 views
Skip to first unread message

Django

unread,
Feb 16, 2018, 3:29:01 AM2/16/18
to django-...@googlegroups.com
#29138: Admin autocomplete field doesn't support the 'to_field' on Foreign Keys
-----------------------------------------+------------------------------
Reporter: Jonathan Nye | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Keywords: autocomplete
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------------
Hi,,

I have encountered an issue where I have specified a 'to_field' in my
foreign key and the autocomplete widget tries to enter the primary key of
the related model instead. This means I cannot submit the form and I get
the "Select a valid choice. That choice is not one of the available
choices." error.

In the AutocompleteJsonView:


{{{

def get(self, request, *args, **kwargs):
"""
Return a JsonResponse with search results of the form:
{
results: [{id: "123" text: "foo"}],
pagination: {more: true}
}
....
....
return JsonResponse({
'results': [
{'id': str(obj.pk), 'text': str(obj)}
for obj in context['object_list']
],
'pagination': {'more': context['page_obj'].has_next()},
})
}}}


Is there a way to replace the id manually when saving the form?

Thanks.

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

Django

unread,
Feb 16, 2018, 10:02:24 AM2/16/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------

Reporter: Jonathan Nye | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Tim Graham):

* cc: Johannes Hoppe (added)
* stage: Unreviewed => Accepted


Comment:

I think the correct solution is to modify `AutocompleteJsonView` to return
the `to_field` value instead of the primary key. To prevent improper data
leakage, `ModelAdmin.to_field_allowed()` should be called.

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

Django

unread,
Feb 16, 2018, 11:43:24 AM2/16/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Johannes Hoppe):

Hi Jonathan,

very good catch. This is certainly a bug. Custom to_fields should be
supported.

Do you want to fix this yourself or should I?

Best
-Joe

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

Django

unread,
Feb 16, 2018, 12:40:16 PM2/16/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Jonathan Nye):

Replying to [comment:2 Johannes Hoppe]:


> Hi Jonathan,
>
> very good catch. This is certainly a bug. Custom to_fields should be
supported.
>
> Do you want to fix this yourself or should I?
>
> Best
> -Joe

Hi Joe,

I can give it a shot. I haven't contributed to Django before so I would
appreciate some pointers to get me looking in the right direction.

What would need to be updated?

Regards,
Jonathan

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

Django

unread,
Feb 17, 2018, 9:04:02 AM2/17/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Johannes Hoppe):

Hey Jonathan,

well that sounds like the perfect opportunity to get into contributing to
Django.

Of course the is a contributing guide, but it long. I would recommend to
read at least the section for new contributors.
https://docs.djangoproject.com/en/dev/internals/contributing/

Anyhow, this is how I would go about this:
Write your test first. You should write a test, that exploits the bug you
found and fails. You will need to add one at the end anyways, if you start
with it actually fixing the issue will become a lot easier. You might want
to add the test here
`tests.admin_views.test_autocomplete_view.AutocompleteJsonViewTests`.

Once that is out of the way there is this line in
`django.contrib.admin.views.autocomplete.AutocompleteJsonView#get`. It
returns a `JsonResponse` that includes the `obj.pk` at some point. What it
should return is the value of the `to_field`. This is where things might
become tricky. Since there is only once view per model and probably
multiple other models having foreign keys to that one, you will need to
get the `to_field` from the relation or pass it as a GET parameter. In the
latter case you should definitely validate the field to avoid leakage as
Tim said earlier.

Anyhow, just get started and let me know if you hit an obstacle and need
help :)

Best
-Joe

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

Django

unread,
Mar 16, 2018, 6:08:42 PM3/16/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+--------------------------------------
Reporter: Jonathan Nye | Owner: Basu Dubey
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Basu Dubey):

* status: new => assigned
* owner: nobody => Basu Dubey


Comment:

I wish to take this up.

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

Django

unread,
Mar 18, 2018, 4:44:35 PM3/18/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+--------------------------------------
Reporter: Jonathan Nye | Owner: Basu Dubey
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Tobi):

* cc: Tobi (added)


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

Django

unread,
Mar 19, 2018, 6:04:53 AM3/19/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+--------------------------------------
Reporter: Jonathan Nye | Owner: Basu Dubey
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Johannes Hoppe):

Cool, let me know when you have patch. I am happy to review it :)

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

Django

unread,
Sep 19, 2018, 9:51:22 AM9/19/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+--------------------------------------
Reporter: Jonathan Nye | Owner: Basu Dubey
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by rkakrik):

Is there any progress?

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

Django

unread,
Oct 2, 2018, 1:31:46 PM10/2/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+--------------------------------------
Reporter: Jonathan Nye | Owner: Basu Dubey
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by Constantino Schillebeeckx):

I've put together a
[https://github.com/ConstantinoSchillebeeckx/django/tree/fix-29138 bug
fix] for this.

'''Note''' I've been able to update all unit tests and create some new
ones, however I haven't been able to leverage `to_field_allowed` to
prevent data leaks. I've tried to implement it (see
[https://github.com/ConstantinoSchillebeeckx/django/blob/fix-29138/django/contrib/admin/views/autocomplete.py#L29
here]), however I can't get it to play nicely with the unit tests. When
uncommented, the `id_field` isn't properly being considered as
to_field_allowed. I'm not familiar with this function, so could use some
help troubleshooting.

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

Django

unread,
Oct 4, 2018, 9:32:43 AM10/4/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: (none)

Type: Bug | Status: new
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Johannes Hoppe):

* owner: Basu Dubey => (none)
* status: assigned => new


Comment:

Replying to [comment:9 Constantino Schillebeeckx]:


> I've put together a
[https://github.com/ConstantinoSchillebeeckx/django/tree/fix-29138 bug
fix] for this.
>
> '''Note''' I've been able to update all unit tests and create some new
ones, however I haven't been able to leverage `to_field_allowed` to
prevent data leaks. I've tried to implement it (see
[https://github.com/ConstantinoSchillebeeckx/django/blob/fix-29138/django/contrib/admin/views/autocomplete.py#L29
here]), however I can't get it to play nicely with the unit tests. When
uncommented, the `id_field` isn't properly being considered as
to_field_allowed. I'm not familiar with this function, so could use some
help troubleshooting.

Hey there! Thanks for the work. It is a little hard to review since you
only posted a branch and I don't know what commits to review. Could you
maybe assign yourself and open up a pull-request to the Django repo? I
will gladly do the first review.

Best
-Joe

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:10>

Django

unread,
Oct 8, 2018, 2:40:40 PM10/8/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner:
| Constantino Schillebeeckx
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Constantino Schillebeeckx):

* cc: Constantino Schillebeeckx (added)
* owner: (none) => Constantino Schillebeeckx


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:11>

Django

unread,
Oct 15, 2018, 4:35:41 PM10/15/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner:
| Constantino Schillebeeckx
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Constantino Schillebeeckx):

Replying to [comment:10 Johannes Hoppe]:


> Hey there! Thanks for the work. It is a little hard to review since you
only posted a branch and I don't know what commits to review. Could you
maybe assign yourself and open up a pull-request to the Django repo? I
will gladly do the first review.

All set: https://github.com/django/django/pull/10494

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:12>

Django

unread,
Oct 15, 2018, 5:38:30 PM10/15/18
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner:
| Constantino Schillebeeckx
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:13>

Django

unread,
Feb 26, 2019, 9:36:40 AM2/26/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Johannes Hoppe
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------
Changes (by Johannes Hoppe):

* owner: Constantino Schillebeeckx => Johannes Hoppe


Comment:

This has been inactive and I stumbled upon a solution while fixing another
issue, I am assigning this to me.

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:14>

Django

unread,
Mar 9, 2019, 7:14:35 AM3/9/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Johannes Hoppe
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------
Changes (by Johannes Hoppe):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:15>

Django

unread,
Apr 13, 2019, 6:58:11 AM4/13/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Johannes Hoppe
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------
Changes (by Florian Apolloner):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:16>

Django

unread,
Oct 29, 2019, 8:36:35 PM10/29/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: schwtyl
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by schwtyl):

* cc: schwtyl (added)
* owner: Johannes Hoppe => schwtyl
* has_patch: 0 => 1


* needs_better_patch: 1 => 0


Comment:

This work went stale, so I re-forked, upmerged from master, and resolved
the merge conflict and isort/flake8 issues. This new PR should be ready
for review/acceptance.

[https://github.com/django/django/pull/11990 New PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:17>

Django

unread,
Oct 29, 2019, 9:04:01 PM10/29/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: schwtyl
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Johannes Hoppe):

* needs_better_patch: 0 => 1


Comment:

Hi schwtyl,

thanks for your commitment. I must admit, I did push this issue aside a
bit in favor of some other patches I have been working on.

Sadly, you did base your work on GH@10494 not on GH@11026, therefore my
critique stays the same. It will only solve this issue but make it harder
to solve #29138 which is equally important.

Might I also suggest next time, to comment on the ticket first, before
fixing someone else's patch. I might save you some trouble.

Best
-Joe

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:18>

Django

unread,
Oct 30, 2019, 1:28:54 AM10/30/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: schwtyl
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by schwtyl):

Joe,

Constantino developed this patch for the project that I succeeded him on,
so I'm hardly a random bystander. Everytime our project builds for the
past year, it pulls it from this GitHub commit, and I'd love to see that
eventually go back to using an official Django release again. I'm not sure
how I'd know about 11026, since you didn't link it here. I don't quite
know that you mean, because this issue *is* #29138. The patch fixes this
issue - I'm not sure which other issue you're referencing, but can you
provide feedback on what can be done for this to be accepted? What
specifically *needs improvement*?

Thanks,

Tyler

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:19>

Django

unread,
Oct 30, 2019, 5:00:12 AM10/30/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------
Reporter: Jonathan Nye | Owner: schwtyl
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------

Comment (by Johannes Hoppe):

Hey Tyler,

thanks for your swift response. I see, I wasn't ware of your background,
but also didn't mean to offend you. I my comment really was only trying to
save you some effort. I know first hand that the Django development
process can be lengthy and demotivating.

Anyhow, I see where you are coming from, and yes, you shouldn't absolutely
use a mainstream Django version, to avoid security vulnerabilities.
Might I suggest to use django-select2 until then? I developed and maintain
that package, it was the blue print for Django's autocomplete field
implementation, which I implemented too. It does work slightly different,
but it will at least allow you to jump to a stable Django release.

Regarding the patch: As the initial developer of this feature I have an
elevated interest to solve all bugs, not just this one. That aside – IMHO
– the best solution actually happens to fix both problems.

I will do a review of your PR, to give you a bit better feeling of the
work that would need to be done on the PR to fix only this issue. I'll let
you decide, if you still think it's worth the effort given that all
changes would need to be revered with the fix for #29138.

Best
-Joe

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:20>

Django

unread,
Oct 30, 2019, 11:29:39 AM10/30/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Tyler Schwartz
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------

Comment (by Johannes Hoppe):

Scratch everything I said. I had another look at #29138. It will require a
lot of work and probably some mailing list discussion. Therefore, I do
believe it's worth the effort to fix them separately even if it involves
revering some parts later.

I already did a first review of your patch. I'll give it another go
tomorrow. I believe we can push this over the finish line.

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:21>

Django

unread,
Oct 30, 2019, 5:18:37 PM10/30/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Tyler Schwartz
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------

Comment (by Tyler Schwartz):

Thanks Joe!

I appreciate the background you were able to fill in, and I wasn't aware
that you were so involved in the implementation of Django Admin's
autocomplete feature. I am new to Django development, though I am
interested in helping take this forward if I can be of help, even if it
takes a while. I don't have any emotional attachment to this current
patch, other than having used it in production for this past year (yikes!)
- so if you think a different direction is preferable, I won't be
offended. I'll take a look at your feedback shortly.

Regardless, you make a good point - I am working to transition our code
back to a stable Django release (I inherited this setup when I started
earlier this year).

-Tyler

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:22>

Django

unread,
Nov 15, 2019, 5:50:08 AM11/15/19
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Tyler Schwartz
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------

Comment (by Manel Clos):

We have been using this in Django 2.1.5. As the to_field contains a code,
it is a valid integer, and no error is produced. This issue should be
marked as DATA LOSS and be urgently addressed.

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:23>

Django

unread,
May 26, 2020, 4:09:13 AM5/26/20
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Tyler Schwartz
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------
Changes (by Carsten Fuchs):

* cc: Carsten Fuchs (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:24>

Django

unread,
Nov 27, 2020, 11:58:53 AM11/27/20
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------+------------------------------------------
Reporter: Jonathan Nye | Owner: Johannes Maron
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0

Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------------
Changes (by Johannes Maron):

* owner: Tyler Schwartz => Johannes Maron


* needs_better_patch: 1 => 0


Comment:

Since we have a working patch, that's been reviewed a bunch, I'm snatching
this ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:25>

Django

unread,
Jan 7, 2021, 6:00:51 AM1/7/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------

Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: assigned
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution:
Keywords: autocomplete | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:26>

Django

unread,
Jan 12, 2021, 5:38:05 AM1/12/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution: fixed

Keywords: autocomplete | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"3071660acfbdf4b5c59457c8e9dc345d5e8894c5" 3071660a]:
{{{
#!CommitTicketReference repository=""
revision="3071660acfbdf4b5c59457c8e9dc345d5e8894c5"
Fixed #29010, Fixed #29138 -- Added limit_choices_to and to_field support
to autocomplete fields.

* Fixed #29010 -- Added limit_choices_to support to autocomplete fields.
* Fixed #29138 -- Allowed autocomplete fields to target a custom
to_field rather than the PK.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:27>

Django

unread,
Mar 18, 2021, 9:06:24 AM3/18/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: autocomplete | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"0b120f5d649fb21bb898a8b834b6b33e4cdd9504" 0b120f5]:
{{{
#!CommitTicketReference repository=""
revision="0b120f5d649fb21bb898a8b834b6b33e4cdd9504"
Refs #29138 -- Added extra tests for autocomplete FK to_field variations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:28>

Django

unread,
Apr 8, 2021, 5:57:22 AM4/8/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: autocomplete | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by 007):

AutocompleteMixin is incompatible in django 3.1 and 3.2. and there is no
tip like RemovedInDjango32Warning in django 3.1. It's not user friendly.

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:29>

Django

unread,
Apr 8, 2021, 6:04:03 AM4/8/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: autocomplete | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Johannes Maron):

Hi 007,

Thank you for reaching out. I understand your frustration. However, the
`AutocompleteMixin` was never documented, which means it is not considered
a public API. Private APIs are not subject to deprecation. That is the
risk you take, when using a private API.

All that being said, is there anything I could be of help with? If you
have a 3rd party package, that needs assistance, I would be delighted to
help.

Best,
Joe

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:30>

Django

unread,
Apr 8, 2021, 9:00:33 PM4/8/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: autocomplete | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by 007):

Replying to [comment:30 Johannes Maron]:


> Hi 007,
>
> Thank you for reaching out. I understand your frustration. However, the
`AutocompleteMixin` was never documented, which means it is not considered
a public API. Private APIs are not subject to deprecation. That is the
risk you take, when using a private API.
>
> All that being said, is there anything I could be of help with? If you
have a 3rd party package, that needs assistance, I would be delighted to
help.
>
> Best,
> Joe

In my package to achieve a similar function of this submission. I'm going
to try to re implement it in the way of 3.2, and compatible with earlier
versions

https://github.com/007gzs/django-
cool/blob/f13d52b571eb77c02b062523bc1d79cdeed31b5a/cool/admin/widgets.py#L184-L234

https://github.com/007gzs/django-
cool/blob/f13d52b571eb77c02b062523bc1d79cdeed31b5a/cool/admin/views.py#L14-L76

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:31>

Django

unread,
Apr 8, 2021, 10:22:57 PM4/8/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: autocomplete | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by 007):

I have change my package in https://github.com/007gzs/django-
cool/commit/63b0ea6021ea50ad40a2d5d0af6ae9c6cbcdab0f

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:32>

Django

unread,
Apr 12, 2021, 6:46:49 AM4/12/21
to django-...@googlegroups.com
#29138: Add ModelAdmin.autocomplete_fields support for ForeignKeys that use
to_field
-------------------------------------+-------------------------------------
Reporter: Jonathan Nye | Owner: Johannes
| Maron
Type: Bug | Status: closed
Component: contrib.admin | Version: 2.0
Severity: Normal | Resolution: fixed
Keywords: autocomplete | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Johannes Maron):

I am happy you found a solution. And this should be the last big change on
this feature for a while.

--
Ticket URL: <https://code.djangoproject.com/ticket/29138#comment:33>

Reply all
Reply to author
Forward
0 new messages