Steve, the choices are different for each product in the shop. some choices are clothing sizes, some physical sizes etc.
So with that in mind i could go with a bunch of choices hooked up to a choices field if i can tell the form which choice field to use based around the product attribute stored. but what if im not the shop admin? in that case i'd have to amend the code every time something goes out of stock in a certain size. so i came upon the plan to have a boolean denote whether or not the object has a size option and if so store the options in the model so that it could be amended in the admin pages. then draw a select at runtime with the right options in it. thanks for the other pointers, i had an idea the function wasn't right and indeed it isn't going to work at all since the widget won't allow a callable. i'll try turning that function into a method on the form and see if it won't work that way Matt On 3 Aug, 15:26, Steve Holden <holden...@gmail.com> wrote: > 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.