我需要 OrderAmount 下的选择字段来根据 StoreLiquor 中的 PackSize 显示不同的选择。我想我大概知道代码应该是什么样子,但我担心我的查询可能是错误的,而且我也不确定你把代码放在哪里。这是代码:
g = StoreLiquor.objects.all()
a = g.BottleSize
b = g.PackSize
c = b*2
d = b*3
e = b*4
f = b*5
if a == "1750 ML":
pack_size = (
('one', '1')
('three', '3')
('reg', b )
('reg1', c )
('re2', d )
('re3', e )
('re4', f )
)
elif a == "1000 ML":
pack_size = (
('1', '1')
('3', '3')
('6', '6')
('reg', b )
('reg1', c )
('re2', d )
('re3', e )
('re4', f )
)
elif a == "750 ML":
pack_size = (
('1', '1')
('3', '3')
('6', '6')
(b, b)
(c, c)
(c, d)
(e, e)
(f, f)
)
elif a == "375 ML":
pack_size = (
('3', '3')
('6', '6')
('12', '12')
(b, b)
(c, c)
(c, d)
(e, e)
(f, f)
)
elif a == "200 ML":
pack_size = (
('12', '24')
('24', '24')
(b, b)
(c, c)
(c, d)
(e, e)
(f, f)
)
else:
pack_size = (
(b, b)
(c, c)
(c, d)
(e, e)
(f, f)
)
该模型:
class LiquorOrder(models.Model):
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)
StorePrice = models.DecimalField('Store Price', max_digits=5, decimal_places=2)
视图(一旦我让代码工作,这将对它进行一些调整):
def storeliquor(request, store_id, liquor_id):
a = StoreLiquor.objects.get(StoreLiquorID=liquor_id)
s = Store.objects.get(StoreID=store_id)
x = Order.objects.get(storeID=s, Active=True)
d = a.OffPremisePrice
e = request.POST.get('OrderAmount', '')
if request.method == "POST":
f = AddToOrderForm(request.POST)
if f.is_valid():
c = f.save(commit=False)
c.TotalPrice = (float(d)) * (float(e))
c.storeliquorID = a
c.orderID = x
c.save()
return HttpResponseRedirect('/stores/get/%s' % store_id)
else:
f = AddToOrderForm()
args = {}
args['liquor'] = a
args['s'] = s
args['form'] = f
return render(request,'storeliquor.html', args)
我的表格:
class AddToOrderForm(forms.ModelForm):
class Meta:
model = LiquorOrder
fields = ('OrderAmount', 'StorePrice')
如果您需要任何其他信息,请告诉我(当我复制和粘贴时,某些缩进可能已被丢弃。)