Django: choice field with an if then statement

1.2k Views Asked by At

I need the choice field under OrderAmount to display different choices based on the PackSize in StoreLiquor. I think I have approximately what the code should look like, but I fear my query might be wrong, and I'm also not sure where yo put the code. Here is the code:

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)
    )        

the model:

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)

The view (This will have some adjustments made to it once I get the code working):

    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)

My forms:

    class AddToOrderForm(forms.ModelForm):

        class Meta:
            model = LiquorOrder
            fields = ('OrderAmount', 'StorePrice')

Let me know if you need any other info (Some of the indentation may have been thrown off when I copy and pasted it.)

0

There are 0 best solutions below