Django form models | ComboBox value - django

How do I get the selected value from a form's ComboBox field? what is the model class that deals with ComboBoxes? ..
Thanks.

There's no such thing as a ComboBox in Django (or in HTML). I assume you are talking about a ChoiceField, which renders a select control in HTML.
You access the value of a ChoiceField in exactly the same way as any other field, once the form has been submitted and validated - by accessing form.cleaned_data['fieldname'].
You should read the excellent documentation on forms.

As mentioned by #MMRUser, the ChoiceField is the form class to achieve an HTML select element.
But for the model itself, you can pass the choices argument to a model field (typically a CharField) which will result in the ModelForm using an HTML select element.

Related

Where is ID saved in django ModelAdmin autocomplete_fields?

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

Filtering a django choicefield in admin form on the basis of a value of another choicefield in the same form

In the admin form, how do you filter a django choicefield on the basis of value of another choicefield.
For example, if a choicefield is having "fruits" and "vegetables", then on selection of fruits, the second choicefield should have "apple,pear,orange" etc.Similarly one selection of vegetables the field should show "brinjal,lady finger,cabbage"
Thanks in advance
You could probably accomplish that with grouped selects in django-smart-selects. The default is outputting a drop down menu. And you should be able to modify it to output radio buttons instead.

render a CharField as checkbox in Django admin?

Is it possible to render a Django model CharField as a checkbox? I need this checkbox on admin edit page as well as the list_editable list page. E.g. when checked, set this CharField value to 't', else set it to 'n'
You can do a form changing the widgets no matter the type you are using.
Here is the documentation
Then if you need to change other things depending on the input people gives I guess you should do some jQuery creating a $('#mycheckboxid').click function

Django Form won't submit error says Select a Valid Choice ____ is not one of the available choices

I built a form in django and I'm having trouble debugging my Tours Choices. It is a ChoiceField and I use the CheckboxMultipleSelect Widget. I don't know what I'm doing wrong to get the error in the screenshot below. Any thoughts? Do I need to facilitate more information? I'm a django newbie.
Picture of the Form Error
A form.ChoiceField lets you input exactly one choice for a form (out of a selection of several choices). If you saved it to a database in a model there would be one value stored in the database. Your CheckboxMultipleSelect widget is for a form.MultipleChoiceField where you can input multiple values. So change your ChoiceField to a MultipleChoiceField in your form and you should be fine. If you save this data to a model, that model would have to be an appropriate field, like a ManyToManyField if they are Foreign Keys.

Django forms question

I want to generate a form from a model. However one of the fields in the model is a CharField, whose value is mostly "Option1" "Option2" or "Option3". If the user chooses "Other" I would like him to be able to type the value of the field.
How can I do this without having to write a whole form by hand?
I think one only way could be to build your own widget and associate that widget with that CharField on a ModelForm
Django Widgets
UPDATE
Here's a sample custom widget implementation that inherits from TextInput:
http://www.kryogenix.org/days/2008/03/28/overriding-a-single-field-in-the-django-admin-using-newforms-admin