I have a Select widget but I need list all of values about a field of Model, and create new values of these field at the same input.
With select I can list and choose one value, but can't create news.
Whith textinput I can create but not list.
What widget form can I use?
Related
I want to create a foreign key relationship in a model, where it actually provides a select dropdown for the model form based on another field value of the model. Normally dropdown shows the values of __str__ methods return value. I want to keep this also since it used in other places.
For example, consider two models Order and Item.
Item have fields called item_code and item_name.
__str__ will return item_name.
Order model will have foreign key to Item model. But I want it provides the item_code field values in the dropdown for item selection.
How can I achieve this without changing general foreign key behavior?
You would do this in the form, by defining a subclass of ModelChoiceField with a custom label_from_instance method, as described in the documentation.
I have a Django model for a List with a many to many field to Item, it uses a through table, that has a column for quantity.
I want to use Django forms to display a form such that the user can add items to the list. If I just try and create the ModelForm, it shows a selection box, where I can choose items, but there is no way to denote quantity for each item.
I'd like to have it display a dropdown menu where you can select an item, and an input box where you can enter the quantity.
Do I have to do something custom to get it to work this way?
EDIT: I could probably just write the form in HTML myself, but I want to use the validation features of Django forms in the backend too.
It sounds like what you want is an inline formset on the through table. That would give you the selection box for the item and the input box for the quantity.
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.
Does anyone know of a widget that displays 2 select boxes. One shows a list of all object in a model and the other shows the objects which have been selected. The user can then select an object from the first list, click an >> button which moves it to the 'selected' list. Then when the form is saved the objects in the selected list are saved in the manytomany field.
Thanks
django.contrib.admin.widgets.FilteredSelectMultiple
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.