5

send_option(视图中)我有的变量nameSend。我想ID拥有Send

怎么做?谢谢

形式:

class SendOrderForm(forms.Form):
   send_option = forms.ModelChoiceField(queryset=Send.objects.all())

模型:

class Send(models.Model):
    name = models.CharField(max_length=50)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    time = models.CharField(max_length=150)

意见:

if request.method == 'POST':
        form = SendOrderForm(request.POST)
        if form.is_valid():
            send_option = form.cleaned_data['send_option']
4

2 回答 2

10

你能做的是,

if request.method == 'POST':
    form = SendOrderForm(request.POST)
    if form.is_valid():
        send_option = form.cleaned_data['send_option'].id

form.cleaned_data['send_option']会得到对象,你可以id 通过做一个.pk或得到它.id

于 2012-10-02T11:11:02.943 回答
0

您的Send模型是否具有返回实例的方法__unicode__或方法?__str__name

如果是这样,您需要返回id实例而不是name.

于 2012-10-02T11:17:09.773 回答