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