views:

24

answers:

1

I have a form containing a ModelMultipleChoiceField.

Is it possible to come up with a url mapping that will capture a varying number of parameters from said ModelMultipleChoiceField?

I find myself doing a reverse() call in the view passing the arguments of the form submission and realized that I don't know how to represent, in the urlconf, the multiple values from the SELECT tag rendered for the ModelMultipleChoiceField...

+1  A: 

This might not answer 100% of your question but, the technique I use for multivalued parameters in URLs is to pass them as an opaque blob to the view and let that do the decoding.

# URLConf
(r'^foo/(?P<ids>([0-9]+,?)+)/)$', foo),

# View
def foo(request, ids):
    ids=ids.split(',')

# Reverse call
reverse(foo, ','.join(sorted(ids)))

The call to sorted() ensures that equivalent lists of ids produce identical URLs (assuming order of ids isn't significant). You can also make ids a set if you don't want duplicate values.

Dan Head
Thanks much. My brain was obviously momentarily impaired... :-)
celopes