2

I'm trying to set the choices of my CharField in the view based on a bottle's size. The choices are rendered properly, but when I hit submit, I get an error that my choice was invalid. If I remove the dynamic choice setting from the view, and just use a default I set in the models.py file, I still get an invalid error. However, if I remove the choices all together, then the program lets me enter any value, and the submission goes just fine. What am I doing wrong? Am I not defining the choices correctly? 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)
RuSs
  • 769
  • 2
  • 12
  • 27
  • You have to define all of your choices beforehand. Doing it dynamically will fail. – Henrik Andersson Sep 26 '13 at 04:48
  • @limelights But how would I pass the liquor ID into the model? – RuSs Sep 26 '13 at 05:10
  • Since it's a choice field you would have the choice of selecting when saving the model. Take a gander at the [docs](https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices) – Henrik Andersson Sep 26 '13 at 05:26
  • 1
    See this, you can pass choices list to `__init__()` of form to set for a field. http://stackoverflow.com/questions/3419997/creating-a-dynamic-choice-field – Rohan Sep 26 '13 at 05:55

0 Answers0