A Django form field used for representing model relationships
Questions tagged [modelchoicefield]
86 questions
0
votes
1 answer
I have constants defined in a Django model class. How can I access the constants in a template?
I have a model class with a choice field and its possible values defined as constants, as recommended in https://docs.djangoproject.com/en/3.0/ref/models/fields/#choices
class Student(models.Model):
FRESHMAN = 'FR'
SOPHOMORE = 'SO'
…

spst
- 50
- 5
0
votes
1 answer
Modelchoicefield save in database django
I want to create a webapp for managing a library .i create the book and the category model.the category is the class of the book 'history literature ..ect' ,i want the category appears as options when adding the book .so i put it in…

swayli
- 1
- 1
0
votes
1 answer
How do I set a foreignkey field in Django models to return no values?
I have a Django model like:
class hotel(models.Model):
name = models.CharField(max_length=60)
city = models.ForeignKey('city', on_delete=models.DO_NOTHING)
state = models.ForeignKey('state', on_delete=models.DO_NOTHING)
country =…

J J
- 43
- 7
0
votes
1 answer
Displaying fields other than pk in a Django Form with a ModelChoiceField
I am building a website where users can upload files and can attach uploads to projects that they created beforehand. The upload is done with a django form where the user can specify the title, comments, etc... There is also a dropdown list where…

mvb
- 45
- 4
0
votes
1 answer
How can I use choice_fields in my models.py file and how to represent them in my output?
I'm trying to include an attribute in models named as 'action' to show the action status of particular todo items. And for that I've tried to create these files but its not working as per my requirements.
I expected a dropdown or checkboxes but…

Vikash Yadav
- 3
- 1
0
votes
1 answer
App works with ModelChoiceField but does not work with ModelMultipleChoiceField
I am trying to retrieve user input data in a django page. But I am unable to choose to multichoice field. I have tried multiple alternatives to no…
0
votes
2 answers
Select data from drop-down list and save it to database in Django
I am a newbie in Django. I want to show the food_status in drop-down list options, therefore the chef can select one of them, change it, and update it into database. It can be updated into database, but i am not sure how to display the drop-down…

Eve11
- 99
- 3
- 14
0
votes
1 answer
how to create a Django form/formset with Modelchoice fields having values from different querysets
I would like to display simultaneously several modelchoicefields, each of them with data from the same model but having different querysets. To do so shall I use one formset or several forms to pass to the template?
I have tried with the…

Steve
- 97
- 1
- 1
- 9
0
votes
0 answers
Django manytomany choicebox
I would like to implement a choicebox where the user can select the appropriate options and 'transfer' them to a 'selected box' before saving. Django has this for the admin interface , like in this post, but how to implement it in a normal website?…

Spatial Digger
- 1,883
- 1
- 19
- 37
0
votes
1 answer
How to dynamically filter a view by ModelChoiceField selection
I have created a template that renders a dropdown list of students.
I want to filter a query set on another view to show schedule info for just the student that was selected.
perm_id in student_list is primary key and perm in StudentSchedule is…

Scott Norman
- 21
- 5
0
votes
0 answers
Django ModelForm with ModelChoiceField does not appear to work with arguments
Any help would be greatly appreciated as I am pulling my hair out with this one! I have a Formview that sends over a repair_id and a part_id to a ModelForm. The ModelForm takes this data as input for a ModelChoiceField pulling a set of values. I…

9Productions
- 1
- 1
0
votes
1 answer
Passing request.user to a queryset in ModelChoiceFilter
I want to pass request.user to a queryset in ModelchoiceFilter, because choice entry should be limited according to login user group. I try it, but getting kwargs in filters.py did not work.
models.py
class Score(models.Model):
group =…

TS Jee
- 121
- 1
- 4
- 12
0
votes
1 answer
Django ModelChoiceField is not initially selected according to related models
I'm having some difficulties with Django's ModelChoiceField.
I wrote the following code:
class BookForm(ModelForm):
publisher = forms.ModelChoiceField(queryset=Publisher.objects.all())
...
Now Book and Publisher are related this way: Book →…

Yuval
- 31
- 4
0
votes
0 answers
Django ModelChoiceField validation error
I have been trying to create a select input from options based on a Model. I have tried several changes but the error persists. I hope someone can help.
The forms.py code.
transaction_type = forms.ChoiceField(choices=TRANSACTION_TYPE_CHOICES,…

Talha Masoud
- 13
- 5
0
votes
1 answer
ModelChoicesField returns Non-Valid-Choice error, although form is valid
If have a date picker form that filters a set of models (Sonde) and populates a ModelChoicesField. This works correctly in terms of date choice in my app, but on my canvas I constantly get the error:
Select a valid choice. That choice is not one of…

Benjamin Mewes
- 89
- 2
- 10