On 8/3/2010 10:05 AM, shofty wrote:
> consider the following code as part of an online shop (im sort of
> working through jim mcgaws ecommerce book).
> in the view
> 
> if product.has_options:
>     form = ProductSizeOptionsAddToCartForm(request=request,
> label_suffix=':')
> else:
>     form = ProductAddToCartForm(request=request, label_suffix=':')
> 
> then in the forms.py
> 
> def create_choices():
>     if self.product:
>         options = self.product.options
>         output = [u'(']
>         for k, v in self.product.options:
>             option_value = smart_unicode(k)
>             option_label = smart_unicode(v)
>             output.append(u"('%s','%s'),")
>         output.append(')')
>         return output
> 
> class ProductSizeOptionsAddToCartForm(forms.Form):
>     quantity =
> forms.IntegerField(widget=forms.TextInput(attrs={'size':'2',
> 'value':'1', 'class':'quantity', 'maxlength':'5'}),
> error_messages={'invalid':'Please enter a valid quantity.'},
> min_value=1)
>     size_option =
> forms.CharField(widget=forms.Select(choices=create_choices))
>     product_slug = forms.CharField(widget=forms.HiddenInput())
> 
> 
> I have tried to write a function that is called from the Select widget
> and i get the "'function' object is not iterable" error message, which
> is quite obvious. what if every product doesn't have the same options
> as CLOTHING_CHOICES list? I've been trying to work out how to store
> the options in the db (product.options as a comma separated CharField)
> and then pass them into the form as part of the product view.
> 
> Anyone done anything like this?
> 
It isn't clear to me why you aren't using a ChoiceField here, which
would seem to make more sense than a CharField.

If you used a ChoiceField then you could supply choices directly to it,
and those choices could in turn be passed to the __init__() method of
your form (which would also need to call forms.Form.__init__() to ensure
the necessary initializations occurred).

That way, as long as you instantiate the form each time it is used
(clearly you can't expect to use the same form with different choices -
tweaking an exisint form that way isn't advisable).

The choices object needs to be an iterable (tuple or list, usually) of
(value, display) pairs, so the only way that your function might work is
if you applied eval() to the current return value, but again that's an
unnecessary complication. Also note that your create_choices() function
returns None if the product has no choices. The choices value needs to
be the result of a *call* to some function, not a function.

Further, it's not really clear from the code you present whether
create_choices() is supposed to be a function or a method. It refers to
self, but it's not obvious how that reference is satisfied. If it's a
form method then it should take self as an argument.

So it's hard to suggest what will work, but I hope I have given you a
few pointers as to why your current code doesn't. Work through it slowly
and deliberately and you'll get there in the end.

regards
 Steve

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to