This is a pretty common problem. Each form object gets instantiated as
you need them.
So in your POST handling, you need to instantiate the form with the
same product field
so that it can populate the choice field setting correct. i.e.
FormForOptionQuantity(product,request)

P.K.


On Dec 9, 7:31 am, pinco <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm trying to populate dynamically at runtime a choicefield and
> subsequently validate the user choice, but without results.
> The goal is to permit the user, in an e-commerce application, to
> choose the number of products to buy through a choicefield, with a
> list of integer from 1 to a maximum number set to the amount of
> products in stock (i.e. if I have 5 products in stock, the choice
> should be restricted to 1, 2, 3, 4, 5).
>
> The following snippet actually populated in a right way the
> choicefiled:
>
> class FormForProductQuantity(forms.Form):
>         quantity = forms.ChoiceField()
>
>         def __init__(self, product, *args, **kwargs):
>                 super(FormForProductQuantity, self).__init__(*args, **kwargs)
>                 self.fields['quantity'].choices = [(str(product.id) + "_" + 
> str(c),
> c) for c in range (1, product.stock_quantity)]
>
> The problems come when I try to validate user's choice and add the
> product to the shopping cart with the following snippet:
>
> def add_product_to_cart(request, product_id):
>         if request.method == 'POST':
>                 product = Product.objects.get(pk=product_id)
>                 form = FormForOptionQuantity(request.POST)
>                 if form.is_valid():
>                         ... do something
>
> which raise an the error:
>
> Traceback:
> ...
> Exception Value: 'QueryDict' object has no attribute 'stock_quantity'
>
> I have no idea on how to correct my code.
>
> Thank you in advance for any help.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to