Where is ID saved in django ModelAdmin autocomplete_fields?

151 Views Asked by At

I am rewriting some administration interface to django 2.2, currently using django autocomplete_fields admin feature. Simply said I have ModelAdmin object OrderAdmin, which has nested TabularInline ProductAdmin: variable-length table of products which might be added to order. Each of these ProductAdmin holders just contains ForeignKey to actual product class, with some other attributes.

Now I wonder: where does django store id - ForeignKey - of item selected with autocomplete field? It doesn't mark OPTION in selectbox as selected, and although there is suspicious hidden input field with #cashregisterproduct_set-0-id on page, it doesn't have any value. Or is there some special way how to access it? I was thinking about adding id to __str__ method of model and parsing, but thats just ugly.

Thanks for tip.

EDIT: to make it 100% clear, where from does django get ForeignKey of object selected through autoselect_field, when creating new object from ModelAdmin?

1

There are 1 best solutions below

1
wtdmn On BEST ANSWER

I got misguided thinking that this is managed by django. Selected data might be accessed by using select2 framework:

selected_value = $('.myselectbox').select2().val();

related: https://stackoverflow.com/a/47451658/16268461