ModelForm保存数据给出错误:选择有效选项。该选择不是可用选项之一

时间:2015-11-17 18:36:41

标签: django modelform

Models.py

class ZoneDetail(models.Model):
    zone_id = models.IntegerField(primary_key=True )
    zone_name = models.CharField(max_length =  20 )

    def __str__(self):
        return self.zone_name

class StateDetail(models.Model):
    zoneId = models.ForeignKey(ZoneDetail)
    state_name = models.CharField(max_length =  20 , choices = STATE_CODE )


    def __str__(self):
        return self.id


class Transaction(models.Model):
    zone                = models.ForeignKey(ZoneDetail)
    state               = models.ForeignKey(StateDetail)

    def __str__(self):
        return self.vechile_number

交易模型将数据存储在表单提交中。

forms.py

class TransactionForm(forms.ModelForm):
    zone = forms.ModelChoiceField(queryset = ZoneDetail.objects.all(),  empty_label = "-- Select Zone --")
    state_name = forms.ModelChoiceField(queryset = StateDetail.objects.none())

     class Meta:
         model = Transaction
         fields = ['zone','state_name']

HTML

<form method="POST" action = "." >{% csrf_token %}
            <table  border = "0" align="left" margin: "5" > 

                    <tr> 
                        <td style="font-size: 15px; border-bottom: 0px " > {{ form.zone.label_tag }}{{ form.zone }} &nbsp; </td> 
                    </tr>
                    <tr> 
                        <td style="font-size: 15px; border-bottom: 0px "> {{ form.state_name.label_tag }}{{ form.state_name }}</td>
                    </tr>

                    <input value="Save" title="Save" type="submit" id="btn_save"> 
            </table>    
    </form>

表单提交给我错误:选择有效选项。这个选择不是可用的选择之一

0 个答案:

没有答案