select a valid Choice While using ModelChoiceField and queryset

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']

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

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

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

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

Nothing going on in the template
Just running
Form.as_p

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

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

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

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.

1 Like

First Model :

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 :

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 :

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 :

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']

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.

1 Like

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.

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

ChoiceField - See:

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

2 Likes

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.

Read the docs for choices - it does not say that the choices attribute takes a list.

What do the docs say choices must be?

1 Like

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

Close.

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

A 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?

I have not used the getitem()

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

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