views:

770

answers:

3

The default django 1.0.2 ManyToManyField widget (a multi-select) is difficult to use when there are a lot of things in the select box. Is there another widget available that gives a comma separated list of id's in a textarea? If this is not available what do I need to do to write one, and have it show up on ModelForm.as_p() and in the admin site?

A: 

In the Admin you can use filter horizontal and/or filter vertical:

class MyModelAdmin(admin.ModelAdmin):
    filter_horizontal = ['many_to_many_field_name']
    filter_horizontal = ['another_many_to_many_field_name']

doc

Andrea Di Persio
Thanks, but that is more of the same. That lead me to the ManyToManyRawIdWidget, which is closest to what I'm looking to use. I think I'll start with the ManyToManyRawIdWidget as a base to build up what I need.
dar
+1  A: 

If there are no existing widgets that do what you want (and I don't think there are) then you'll need to write your own. Unfortunately, the Django documentation doesn't show you how to do this, but it's not hard to figure out by looking at the source-code forms/widgets.py copying an existing widget and modifying it.

Disappointing there aren't any out there, this comes close though: http://www.djangosnippets.org/snippets/1365/
dar
A: 

I believe setting raw_id_fields on the manytomanyfield actually outputs a TextInput widget with a comma-separated list of ids. You may just override this in admin.py, in the corresponding ModelForm and force a TextArea widget on it.

Ricardo Mendonca
Looks like this is only for the admin app. Thanks, but not useful for this question.
dar