django在视图中设置CharField的选项

时间:2013-09-26 04:38:59

标签: django django-models django-forms django-views

我正在尝试根据瓶子的大小设置我的CharField在视图中的选择。选项正确呈现,但是当我点击提交时,我收到一个错误,我的选择无效。如果我从视图中删除动态选择设置,并且只使用models.py文件中设置的默认设置,我仍然会收到无效错误。但是,如果我一起删除所有选项,那么程序允许我输入任何值,并且提交正常。我究竟做错了什么?我没有正确定义选择吗? models.py

class LiquorOrder(models.Model):

    pack_size = (
                ('7', '7'),
                ('7', '7'),
            )

    LiquorOrderID = models.AutoField(primary_key=True)
    storeliquorID = models.ForeignKey(StoreLiquor)
    orderID = models.ForeignKey(Order)
    OrderAmount = models.PositiveSmallIntegerField('Order Amount', max_length=3, choices=pack_size)
    TotalPrice = models.DecimalField('Total Price', max_digits=5, decimal_places=2)

views.py

def storeliquor(request, store_id, liquor_id):

    LiquorID = Liquor.objects.get(id=liquor_id)
    storeliquor = StoreLiquor.objects.get(liquorID=liquor_id)
    store = Store.objects.get(StoreID=store_id)
    ActvOrder = Order.objects.get(storeID=store, Active=True)
    price = LiquorID.OffPremisePrice
    btlsize = LiquorID.BottleSize

    amount = request.POST.get('OrderAmount', '')
    packD = LiquorID.PackSize
    packX2 = packD*2
    packX3 = packD*3
    packX4 = packD*4
    packX5 = packD*5
    if btlsize == "1750 ML":
        pack_size = (
            ('1', '1'),
            ('3', '3'),
            (packD, packD),
            (packX2, packX2),
            (packX3, packX3),
            (packX4, packX4),
            (packX5, packX5),
        )
    elif btlsize == "1000 ML":
        pack_size = (
            ('1', '1'),
            ('3', '3'),
            ('6', '6'),
            (packD, packD),
            (packX2, packX2),
            (packX3, packX3),
            (packX4, packX4),
            (packX5, packX5),
        )
    elif btlsize == "750 ML":
        pack_size = (
            ('1', '1'),
            ('3', '3'),
            ('6', '6'),
            (packD, packD),
            (packX2, packX2),
            (packX3, packX3),
            (packX4, packX4),
            (packX5, packX5),
        )
    elif btlsize == "375 ML":
        pack_size = (
            ('3', '3'),
            ('6', '6'),
            ('12', '12'),
            (packD, packD),
            (packX2, packX2),
            (packX3, packX3),
            (packX4, packX4),
            (packX5, packX5),
        )
    elif btlsize == "200 ML":
        pack_size = (
            ('12', '12'),
            ('24', '24'),
            (packD, packD),
            (packX2, packX2),
            (packX3, packX3),
            (packX4, packX4),
            (packX5, packX5),
        )
    else:
        pack_size = (
            (packD, packD),
            (packX2, packX2),
            (packX3, packX3),
            (packX4, packX4),
            (packX5, packX5),
        )

    if request.method == "POST":
        OrderForm = AddToOrderForm(request.POST)
        if OrderForm.is_valid():
            formfields = OrderForm.save(commit=False)
            formfields.TotalPrice = (float(amount)) * (float(price))
            formfields.storeliquorID = storeliquor
            formfields.orderID = ActvOrder
            formfields.OrderAmount = amount
            formfields.save()
            return HttpResponseRedirect('/stores/get/%s' % store_id)

    else:
         OrderForm = AddToOrderForm()
         OrderForm.fields['OrderAmount'].choices = pack_size

    args = {}

    args['liquor'] = LiquorID
    args['s'] =  store
    args['form'] = OrderForm


    return render(request,'storeliquor.html', args)

0 个答案:

没有答案