Django ModelForm with Many to Many Field - django

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.

Related

How to get list of all fields corresponding to a model saved in content type framework in django

I have this requirement in which I have to show two dropdown fields on Django admin page.
The first field will be the dropdown of all the table names available in my project, and the second field will be a dropdown all the available fields of the table selected in 1st dropdown. The second field will be dynamic based on the selection of the first dropdown. I am planning to handle this by overriding change_form_template.
Now, I can show the dropdown of table names by fetching them through content type Django model, But not able to fetch corresponding fields as content type model save the model name as a string. So is there any way not necessarily using Content-Type to achieve such a requirement?
Any help around that will be highly appreciated.
Thank you.
Here you have dependency of another fields right so i think you need to see i give link,then yo get error let me know
link:hint of dependency field in django

Make a model field choose from a database of users

So, I'm making an item checkout app for our internal group. So, my django app has a model of an item, with one of the fields being loanedTo.
We have a database of users in the building, and I would like to set the choices field of this loanedTo field to take values from the db. So, when someone checks out an item, he can select from a list of users.
I can't seem to find how to do this exactly?
On here, https://docs.djangoproject.com/en/2.0/ref/models/fields/, the choices field can be set to an iterable list or tuple.

Create input text fields based on value user selects in dropdown in Django forms

In my application, on page loads by default there is a drop-down with value '3' and three input text fields.
If user selects value '2' from drop-down then number of input text fields should be changed from 3 to 2.
If user selects value '5' from drop-down then number of input text fields should be changed from prev. value to 5.
How could I do that dynamically in Django project as I am wanted to create a form in django inside forms.py?
There is a few different ways, but in any case you need to control a number of input fields by JavaScript. You didn't show us you model so we can't give you the best solution, but you have a following options:
if your form is model form, create a form with all possible fields and display/hide fields based on selected value from drop down.
you can use a formset_factory if your input fields belongs to the same model
you can add as many fields as you need by JS and process the form fields in view. This answer can be used as reference
I hope it helps

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.

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.