Hello, I was wondering if I could get some advice for the following problem I am trying to solve. Is it possible to have a form field (or widget) render different html elements depending on different situations?
To help illustrate my point, I have come up with the following fake scenario which is similar to what I want to do. Suppose you have a database table with product information. Lets say you have different types of Shoes, Nails, and Rulers. The Shoes can have multiple sizes (9,10,12,etc.) , The Nails are ordered by the pound, and you can select any size you want as long as it is between the range (25-300 pounds). The Rulers only come in one size. If I wanted to use the same html form for all three products, I would normally have to use the same html field for size for all three and this doesn't quite work out. If I use a text field, It would work but I would have to have complex validations to make sure they type in a value that is correct, and that isn't ideal. I could use a pull down box, but displaying every value between 25-300 in a pull down would be pretty long, and having a pull down value for one element is also annoying. Ideally I would like to use a different element for each situation, I would like to use a hidden field for the ruler since they don't need to pick the value, but we still need the size. For shoes I would like to use a pull down, and for Nails, I would like to use a text field. My first thought was to create a custom widget and I would pass in some parameters and depending on those parameters it would display the appropriate HTML form element. I started down this road, and the code doesn't seem very clean, so either I am doing it wrong, or what I am doing isn't supported. The complex part that I am trying to figure out is how do I pass the parameters that I need from the view to the Custom Widget? I have this working, but it looks very ugly. I also thought about doing this as a template tag, but I am not sure if this is any better. The main downfall for this approach is that I don't think that I can use any of the django form stuff when I do it this way. Or could I? Has anyone done something like this before? Does anyone have a better way to do this? Any advice that you could give, would be great. Thanks, Ken Cochrane Note: In my real life example I get my Product Size from a REST web service, but I figured the database table would make it easier to explain. (this is only pseudo code) excuse the formatting. Models: class ProductSize(models.Model): values = models.CharField(max_length=50) # The values could be "25" , or "25,50,100" , "25-300" type = models.CharField(max_length=50) # types could be "fixed" , "multi" , "range" class Product(models.Model): sizes = models.ForeignKey(ProductSize) name = models.CharField(max_length=50) description = models.CharField(max_length=50) class Order(models.Model): size = models.CharField(max_length=50) # I don't want the foreign key, I just want the value. name = models.CharField(max_length=50) # name of product order_date = models.DateTimeField() # date of order ModelForm: class ProductForm(ModelForm): size = CharField(widget=TextInput()) name = CharField(widget=TextInput()) description = CharField(widget=Textarea()) View : def product_view(request): product_form = ProductForm() return render_to_response({'product_form': product_form}) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---