Maybe I am being completely dumb, but I cannot figure this out.
I have built a test scenario for my situation.
I wanted to store multiple pizzas and the corresponding ingredients. The list of ingredients are stored in a reference table.
test_table_pizza = name of pizza
test_table_ingredients = linked to table above with a link to ingredients in below table
test_table_ingredient = list of ingredients I want user to select from for each pizza.
When the form is loaded, the ingredients are not marked selected, and when you save the form I get a validation error
Select a valid choice. That choice is not one of the available choices
Like I said above I may being dumb, but I cannot figure this out
Models.py
class test_table_ingredient(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class test_table_pizza(models.Model):
name = models.CharField(max_length=200)
ingredients = models.ManyToManyField(test_table_ingredient, through='test_table_ingredients')
def __str__(self):
return self.name
class test_table_ingredients(models.Model):
pizza = models.ForeignKey(test_table_pizza, on_delete=models.CASCADE)
ingredient = models.ForeignKey(test_table_ingredient, on_delete=models.CASCADE)
def __int__(self):
return self.id
Forms.py
class form_pizza(forms.ModelForm):
name = forms.CharField(
label='Pizza Name'
, max_length=100
, required=True
, widget=forms.TextInput(attrs={'class': 'form-control form-label', 'for':'basic-form-name'})
)
ingredients = forms.ModelChoiceField(
label='Ingredients'
, queryset=test_table_ingredient.objects.all()
, empty_label=None
, widget=forms.SelectMultiple(attrs={'class': 'form-select'})
)
class Meta:
model = test_table_pizza
#exclude = ('',)
fields = '__all__'
views.py
def test_pizza(request, pid):
if request.method == 'POST':
form = form_pizza(request.POST)
if form.is_valid():
if int(pid)==0:
f = form.save( commit=False )
f.save()
else:
form = form_pizza(request.POST, instance=test_table_pizza.objects.get(id = pid))
f = form.save( commit=False )
f.save()
else:
form = form_pizza(request.POST)
else:
if int(pid) == 0:
form = form_pizza()
else:
form = form_pizza(instance=test_table_pizza.objects.get(id = pid))
print(form)
return render(request, 'home/view_person/form_pizza.html', {'form':form})
Template
<form id="fAddressType" action="" method="POST" enctype="multipart/form-data" >
{% csrf_token %}
{{ form }}
<br />
<button class="btn btn-primary btn-value" id="master" name="form" type="submit">Save</button>
</form>