Questions tagged [modelchoicefield]

A Django form field used for representing model relationships

ModelChoiceField docs

86 questions
9
votes
1 answer

No Validation on Field Choices Django Postgres?

I created a Student model with field choices. However, when I save it, it doesn't validate whether the choice is in the choices I specified in the model field. Why doesn't it prevent me from saving a new object with a choice I didn't specify in my…
Anatol
  • 3,720
  • 2
  • 20
  • 40
5
votes
1 answer

Django - How to change value of forms.ModelChoiceField

I want to output the value from my Models Name column using forms.ModelChoiceField. I have read a lot of documentation on the subject but its proving difficult to achieve the desired results! Here is my code. I have this form: forms.py from django…
death and gravity
  • 619
  • 1
  • 8
  • 21
4
votes
1 answer

Django Model Choice Field How to return the primary key of the object instead of the display value

I am trying to use the modelchoicefield that Django provides; however, it is not behaving the way I expected. I want the display to the end user to be a string value from my model (which works), but return a the primary key of the Model when the…
jaspearson
  • 81
  • 7
4
votes
2 answers

Custom label for instance with django-filter

I have Observer model which extends User model via OneToOneRelation. I made filtering for model Checklist which has foreign key to Observer. This is the models.py code: class Observer(models.Model): user = models.OneToOneField(User,…
Bulva
  • 1,208
  • 14
  • 28
3
votes
2 answers

Select a valid choice. That choice is not one of the available choices

In my app, i have a dropdown (department) that's depending on the previously selected value from a dropdown(faculty field). I am using ajax to get the new value which works fine. However, when i try saving the form, i get Select a valid choice. That…
3
votes
1 answer

how do I access a django form value in the __init__ method for a query

I have a model that includes a foreign key: class Part(models.Model): partType = models.ForeignKey(PartType, on_delete=models.CASCADE) brand = models.ForeignKey(Brand, on_delete=models.CASCADE) part_name =…
3
votes
0 answers

Django select a valid choice error in form when using custom form template

I've been lurking for an answer for quite some time now, but I haven't found solution for my problems. I made custom template for my form and now, when I try to submit form I get this under choice field: Select a valid choice. is not one of the…
3
votes
4 answers

Display field other than __str__

I am trying to display the version field from the below model other than the default str which is field2_name: Note: This SO link Displaying a specific field in a django form might be more than I need but I am not 100% sure. I tried to implement…
beginAgain
  • 211
  • 4
  • 17
2
votes
1 answer

Formsets. Initial data cannot be displayed in ModelChoiceField

In my application, the user can create and edit reports. When rendering the report edit page, the fields are filled with initial data. I ran into the following problem - one of the fields (operator) does not display the value that I pass to it in…
2
votes
1 answer

ModelChoiceField lists tuples instead of simple values

I have a django form with a ModelChoiceField input and it lists the result as tuples instead of simple values. I have no clue on how to do it. DJANGO class PayThis(forms.Form): amount = forms.FloatField(required=False) cost2 =…
titpoettt
  • 81
  • 7
2
votes
1 answer

ModelChoiceField always raises : "Select a valid choice. That choice is not one of the available choices"

i have a ModelChoiceField that containts clients names selected from my database. but when submiting the form i get : "Select a valid choice. That choice is not one of the available choices" here is my form : class FormVents(forms.Form): …
Elroum
  • 327
  • 1
  • 3
  • 18
2
votes
0 answers

Django ModelMultipleChoiceField Form

On my website, I've got a "Compose Email" page with three fields - Subject, Recipients, & Body. In my database, I've got this custom user model in models.py: class MyUser(models.Model): user = models.OneToOneField(User,…
djr8yk
  • 31
  • 3
2
votes
0 answers

Make Django's ModelChoiceField display as a dropdown with title and image

Below is my Django form. I would like to display a thumbnail and title together in a dropdown element, not as a radio button. With this configuration, it makes my form element a radio button. What am I doing wrong, I cannot figure this out. If I…
user7421798
  • 103
  • 11
1
vote
1 answer

Using integers and strings together in Django choice field

I am trying to use the new Enum types in the latest Django version for the choice field. Specifically I am trying to store the various states of United States as follows: class States(models.TextChoices): ALABAMA = 'AL', 'Alabama' ALASKA =…
Amistad
  • 7,100
  • 13
  • 48
  • 75
1
vote
2 answers

How can I define a ModelSerializer ChoiceField's choices according to object attribute?

My Beta model's stage field provides 5 choices. I want my serializer to not always accept all these choices but only some of them according to the serialized object's actual stage value. For example, if my_beta_object.stage == 1, then the serializer…
1
2 3 4 5 6