Django Forms/Model ValueError: Cannot Assign xxx, must be a xxx instance

В настоящее время я работаю с Django формами. Одно из полей формы - это поле выбора, в котором есть варианты выбора различных вкусов мороженого, которые находятся в базе данных. Ошибка выглядит следующим образом :

ValueError at /orderentry/

Cannot assign "'CHOCOLATE'": "orderInfo.order_Item_Flavor" must be a "flavor" instance.

Вот весь соответствующий код, который я могу вспомнить:

orderentry.forms.py

class customerOrder(forms.ModelForm):
    
    order_Item_Flavor = forms.ChoiceField(choices=flavor.flavor_Choices)
    half_Pint_Count = forms.IntegerField()
    one_Quart_Count = forms.IntegerField()
    pint_Count = forms.IntegerField()
    half_Gallon_Count = forms.IntegerField()
    gallon_Count = forms.IntegerField()

    class Meta:
        model = orderInfo
        fields = ('order_Item_Flavor','half_Pint_Count', 'one_Quart_Count', 'pint_Count', 'half_Gallon_Count', 'gallon_Count',)

inventory.models.py

class flavor (models.Model):
    class Meta:
        verbose_name = "Flavor"
        verbose_name_plural = "Flavors"

    flavor_Choices = [('CHOCOLATE','chocolate'),('VANILLA', 'vanilla'),('COOKIESNCREME', 'cookiesncreme'), ('STRAWBERRY', 'strawberry')]
    flavor = models.CharField(max_length=100, choices = flavor_Choices)

    def __str__(self):
        return '%s Flavor' % self.flavor

orderentry.models.py

class orderInfo (models.Model):
    class Meta:
        verbose_name = "Order Information"
        verbose_name_plural = "Order Information"
    
    order_Item_Flavor = models.ForeignKey('inventory.flavor', on_delete=models.CASCADE)
    half_Pint_Count = models.IntegerField(default=0)
    one_Quart_Count = models.IntegerField(default=0)
    pint_Count = models.IntegerField(default=0)
    half_Gallon_Count = models.IntegerField(default=0)
    gallon_Count = models.IntegerField(default=0)
    cost = models.IntegerField(default=0)
    customer = models.ForeignKey(customerInfo, on_delete=models.CASCADE, default = 0)

    def __str__(self):
        return '%s, Half Pint: %s, Quart: %s, Pint: %s, Half Gallon: %s, Gallon: %s, $%s' % (self.order_Item_Flavor, 
        self.half_Pint_Count, self.one_Quart_Count, self.pint_Count, self.half_Gallon_Count, self.gallon_Count, 
        self.cost)

orderentry.views.py

def getCustomerOrder(request):
    form = customerOrder(request.POST)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            orderentry.forms.order_Item_Flavor = form.cleaned_data['order_Item_Flavor']
            orderentry.forms.half_Pint_Count = form.cleaned_data['half_Pint_Count']
            orderentry.forms.one_Quart_Count = form.cleaned_data['one_Quart_Count']
            orderentry.forms.pint_Count = form.cleaned_data['pint_Count']
            orderentry.forms.half_Gallon_Count = form.cleaned_data['half_Gallon_Count']
            orderentry.forms.gallon_Count = form.cleaned_data['gallon_Count']
            return redirect('continueorder')
    else:
        form=customerOrder()
    
    return render(request, 'orderentry.html', {'form' : form})

Я предполагаю, что это происходит потому, что "CHOCOLATE", полученный через форму, не является буквальным экземпляром "CHOCOLATE", который в настоящее время находится в базе данных. Но я не знаю, как ссылаться на этот экземпляр напрямую.

Любая помощь будет принята с благодарностью. Спасибо!

Вернуться на верх