# select a valid Choice While using ModelChoiceField and queryset

**URL:** <https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437>\
**Category:** Forms & APIs\
**Created:** [January 25, 2023, 6:12pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437 "2023-01-25T18:12:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 6:12pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/1 "2023-01-25T18:12:30Z")

</div>

I want to put a certain object values from one model to another form, and submit it into the latest form. But while submission it shows " select a valid Choice. That choice is not one of the available choices. "  
While i do manage to display the values into the form using

> ModelChoiceField

but i can not submit the form

class buildday1(forms.ModelForm):

```
d1sight = forms.ModelChoiceField(queryset=place.objects.values_list('placeName', flat=True).distinct())
n1stay = forms.ModelChoiceField(queryset=hotel.objects.values_list('hotelName', flat=True).distinct())

class Meta:
    model = day1
    fields = ['code','d1arrive','d1to','d1sight','n1stay']

```

---

<div class="post-metadata">

**Author:** ![cehceh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/cehceh/32/3415_2.png) [@cehceh](https://forum.djangoproject.com/u/cehceh)\
**Post date:** [January 25, 2023, 6:29pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/2 "2023-01-25T18:29:03Z")

</div>

Have you tried to change your queryset to (`place.objects.all()`) instead of (`values_list()`) then see what will happen.  
Your query with (`values_list`) returns string (I thought from the field name"placeName")  
Review the next link on docs  
[Docs #modelchoicefield](https://docs.djangoproject.com/en/4.1/ref/forms/fields/#modelchoicefield)

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 6:35pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/3 "2023-01-25T18:35:14Z")

</div>

Yes, i have tried .all() : problem with this is want only specific field

I also tried .all() with \_is\_field\_name: problem with this is that it returns as   
And doent not display it

---

<div class="post-metadata">

**Author:** ![cehceh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/cehceh/32/3415_2.png) [@cehceh](https://forum.djangoproject.com/u/cehceh)\
**Post date:** [January 25, 2023, 6:41pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/5 "2023-01-25T18:41:59Z")

</div>

You have to show us your template may be there is something wrong.

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 6:42pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/6 "2023-01-25T18:42:32Z")

</div>

Have been looking at it from all angles, but so far nothing ,  
What i need is a way to take particular field values from one model and use it for a form in a choiceField

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 6:43pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/7 "2023-01-25T18:43:39Z")

</div>

Nothing going on in the template  
Just running  
Form.as\_p

---

<div class="post-metadata">

**Author:** ![cehceh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/cehceh/32/3415_2.png) [@cehceh](https://forum.djangoproject.com/u/cehceh)\
**Post date:** [January 25, 2023, 6:44pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/8 "2023-01-25T18:44:27Z")

</div>

> [@ubins](#):
>
> What i need is a way to take particular field values from one model and use it for a form in a choiceField

I don’t understand , Can I see your model ? or Explain what do you try to do?

---

<div class="post-metadata">

**Author:** ![cehceh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/cehceh/32/3415_2.png) [@cehceh](https://forum.djangoproject.com/u/cehceh)\
**Post date:** [January 25, 2023, 6:45pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/9 "2023-01-25T18:45:36Z")

</div>

> [@ubins](#):
>
> Nothing going on in the template  
> Just running  
> Form.as\_p

OK I understood you but I want to understand what do you want to do ?

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 6:50pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/10 "2023-01-25T18:50:53Z")

</div>

Its a story of Two MODELS

One has already taken in data via forms .

Its already in a DB  
So this SECOND MODEL wants to use those values as an viable input ( choiceField Inputs )

It displays the choices all fine  
but when i submit the form  
It shows the Error.

Note: the second Model does not want all the values ( we could have used .all() and fixed the problem) it only wants a particular field “placeName”  
Which is from the first Model

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2023, 7:02pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/11 "2023-01-25T19:02:03Z")

</div>

We need to see the models and the complete forms involved here.

Keep in mind that a reference to another model is done by the primary key of that related model. This means that the select box can show any value as the display value, but must supply the primary key as the value for the selected element.

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 7:10pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/12 "2023-01-25T19:10:38Z")

</div>

First Model :

```auto
from django.db import models
class place(models.Model):
    placeName = models.TextField()
    point1 = models.TextField( null = True, max_length=100, blank = True)
    point2 = models.TextField( null = True, max_length=100, blank = True)
    image1 = models.ImageField( upload_to='placeimages')

```

First Form :

```auto
from django import forms
from .models import place
class placeform(forms.ModelForm):
    placeName = forms.CharField(max_length=50, required=True )
    point1 = forms.CharField(max_length=250, required=False)
class Meta:
        model = place
        fields = ['placeName','point1','point2','image1']

```

> Second Model :

```auto
class day1(models.Model):
 #day 1 info
    code = models.TextField( blank= True, null=True)
    d1arrive = models.TextField( blank=True, null= True)
    d1to = models.TextField( blank=True, null= True)
    d1sight = models.TextField( blank=True, null= True)
    n1stay = models.TextField( blank= True, null= True)

```

Second Form :

```auto
class buildday1(forms.ModelForm):

    d1sight = forms.ModelChoiceField(queryset=place.objects.values_list('placeName', flat=True).distinct())
    n1stay = forms.ModelChoiceField(queryset=hotel.objects.values_list('hotelName', flat=True).distinct())
    
    class Meta:
        model = day1
        fields = ['code','d1arrive','d1to','d1sight','n1stay']

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2023, 7:20pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/13 "2023-01-25T19:20:15Z")

</div>

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.  
(You don’t need to repost your code, but it would be helpful if you edited your previous post to put the ``` before and after it.)

You’re using the wrong form field type for your models here. You have your model defined with `d1sight` and `n1stay` as text fields, but you’ve defined the form fields as `ModelChoiceField`, which implies a ForeignKey relationship.

You need to decide which is right. Either your model fields should become ForeignKey fields, or the form fields should be regular `ChoiceField`.

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 7:26pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/14 "2023-01-25T19:26:43Z")

</div>

I would like to proceed further with the Best Practice in Django.  
But as of now i m not aware of that  
If i change them to normal ChoiceField how would i use the objects and values for input ?  
I guess i need to use foreignKey ?  
I m not sure how to do that here.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2023, 7:35pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/15 "2023-01-25T19:35:49Z")

</div>

You’re close on the formatting. You need to use the backtick - ` character, not the apostrophe - ' or any type of “smart quote”.

ChoiceField - See:

- [ChoiceField](https://docs.djangoproject.com/en/4.1/ref/forms/fields/#choicefield)
- [choices](https://docs.djangoproject.com/en/4.1/ref/forms/fields/#django.forms.ChoiceField.choices)
- [Field.choices](https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.Field.choices)

Briefly, your query will return the list two-tuples and will be used in the `choices` attribute of your ChoiceField.

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 7:40pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/16 "2023-01-25T19:40:50Z")

</div>

i m currently using a mobile phone , cant find the backtick character , right now ( will do it once i m on a computer )

when we use regular ChoiceField we need to use Choices attribute , that takes list as input.  
But how do a put particular field value (object.placeName) into that list , i tried appending it but could not do it properly.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2023, 7:44pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/17 "2023-01-25T19:44:56Z")

</div>

Read the docs for [`choices`](https://docs.djangoproject.com/en/4.1/ref/forms/fields/#django.forms.ChoiceField.choices) - it does _not_ say that the `choices` attribute takes a list.

What do the docs say `choices` must be?

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 7:49pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/18 "2023-01-25T19:49:01Z")

</div>

2- Tuples ?  
If yes how i put placeName values into it ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2023, 7:50pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/19 "2023-01-25T19:50:05Z")

</div>

> [@ubins](#):
>
> 2- Tuples ?

Close.

Specifically, from the first line in the docs for `Field.choices`:

> A [sequence](https://docs.python.org/3/glossary.html#term-sequence) consisting itself of iterables of exactly two items (e.g. `[(A, B), (A, B) ...]` ) to use as choices for this field.

(Sorry, I copied the wrong link)

How can you write a query that returns all rows but contains only two specific fields from a model?

---

<div class="post-metadata">

**Author:** ![ubins](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ubins/32/11923_2.png) [@ubins](https://forum.djangoproject.com/u/ubins)\
**Post date:** [January 25, 2023, 8:00pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/20 "2023-01-25T20:00:03Z")

</div>

I have not used the **getitem** ()

Can .only() or .filter() be used here ?  
I m not sure.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 25, 2023, 8:04pm UTC](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437/21 "2023-01-25T20:04:43Z")

</div>

Hint: How did you create a list with only one field in your original version?

[Next page](https://forum.djangoproject.com/t/select-a-valid-choice-while-using-modelchoicefield-and-queryset/18437.md?page=2)
