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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/049.3ad78babaa2a7eaec5af0222cd744e46%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to