﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29138	Add ModelAdmin.autocomplete_fields support for ForeignKeys that use to_field	Jonathan Nye	Johannes Maron	"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."	Bug	closed	contrib.admin	2.0	Normal	fixed	autocomplete	Johannes Maron Tobi Constantino Schillebeeckx Tyler Schwartz Carsten Fuchs	Ready for checkin	1	0	0	0	0	0
