Pylons and Django newforms — SelectMultiple problems
Pylons has become my framework of choice for any new projects where I get to choose the technology. I’ve had trouble, however, finding a form library I like. Max Ischenko posted an entry about how to get Pylons to work with Django’s newforms library and I’ve been hooked ever since. It just seems to stay out my way better than any other form framework out there.
I ran into a problem a couple of days ago when using the SelectMultiple widget. The MultipleChoiceField expects a list from the SelectMultiple widget. In the Django framework, this isn’t a problem because it checks for a MultiValueDict:
def value_from_datadict(self, data, files, name):
if isinstance(data, MultiValueDict):
return data.getlist(name)
return data.get(name, None)
Unfortunately for me, and any other WSGI framework users, MultiValueDict is a Django-specific datastructure. I also think that isinstance is not a very Pythonic way to handle this. Duck typing seems the correct way to do this and I’ve used it in my patch. It checks to see if the data object has a getall attribute. This tells me its a Paste MultiValueDict object which is more prevalent in other WSGI frameworks, such as Pylons.
Just take the patch I’ve created to fix the SelectMultiple widget and apply it to django/newforms/widgets.py. Now the widget will return your values in a list, exactly how MultipleChoiceField wants it.
Leave a Reply