need a multiselect list box for a static list of options

1,040 views
Skip to first unread message

Adam Fraser

unread,
Mar 23, 2009, 2:49:01 PM3/23/09
to Django users
Hello,

This should be a pretty simple question. I have a list of options:

STAIN_CHOICES = (
(1, 'DNA - DAPI'),
(2, 'DNA - Hoechst'),
(3, 'DNA - other'),
(4, 'Actin - Phalloidin'),
(5, 'Tubulin'),
)

I would like users to be able to select more than one of these things
at a time. It looks like models.CommaSeparatedIntegerField would be
good for this, but how can I get a multi-selection list box as an
input?

-adam

Briel

unread,
Mar 23, 2009, 3:09:05 PM3/23/09
to Django users
Hi.

I haven't played much around with this kind of thing, but I
would suggest that you take a look at the form widgets.
One of them is called, SelectMultiple, which I bet is the one
you are after, but I'm not sure if it will work with choices.
You can find something about it at
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

~Jakob

Adam Fraser

unread,
Mar 23, 2009, 3:25:52 PM3/23/09
to Django users
I don't see anything called SelectMultiple on the page you linked.
That was the page I've been scouring for a while now looking for a way
to do this. The ModelMultipleChoiceField looks promising, but
apparently it's associated with a ManyToManyField model, and that
doesn't really fit what I'm going for, but seems to complicate things,
requiring me to make a whole separate model class and add the
corresponding tables and columns to my database when all I want are a
few static choices.

Thanks for taking the time to respond!

Any other ideas?
-Adam

On Mar 23, 3:09 pm, Briel <toppe...@gmail.com> wrote:
> Hi.
>
> I haven't played much around with this kind of thing, but I
> would suggest that you take a look at the form widgets.
> One of them is called, SelectMultiple, which I bet is the one
> you are after, but I'm not sure if it will work with choices.
> You can find something about it athttp://docs.djangoproject.com/en/dev/topics/forms/modelforms/

Brian Neal

unread,
Mar 23, 2009, 4:33:42 PM3/23/09
to Django users
There is a form widget called SelectMultiple:

http://docs.djangoproject.com/en/dev/ref/forms/widgets/#django.forms.SelectMultiple

You could use a ChoiceField with a SelectMultipleWidget, it would
seem.

-BN

Adam Fraser

unread,
Mar 23, 2009, 4:36:40 PM3/23/09
to Django users
I found the SelectMultiple widget here:

http://docs.djangoproject.com/en/dev/ref/forms/widgets/

But I still don't know how to hook that up to a model like
CommaSeparatedIntegerField.

help?

Brian Neal

unread,
Mar 23, 2009, 4:43:10 PM3/23/09
to Django users
On Mar 23, 3:36 pm, Adam Fraser <adam.n.fra...@gmail.com> wrote:
> I found the SelectMultiple widget here:
>
> http://docs.djangoproject.com/en/dev/ref/forms/widgets/
>
> But I still don't know how to hook that up to a model like
> CommaSeparatedIntegerField.
>
> help?
>

Did you try something like this? This is off the top of my head:

class MyForm(forms.ModelForm):
my_choices = forms.CommaSeparatedIntegerField
(widget=forms.SelectMultiple(choices=STAIN_CHOICES))

See:
http://docs.djangoproject.com/en/dev/ref/forms/widgets/#specifying-widgets

Adam Fraser

unread,
Mar 24, 2009, 9:55:48 AM3/24/09
to Django users
Still doesn't work. Maybe I should be more specific.

I'm editing projectprofiler/projects/models.py which hasn't needed to
import forms for anything, and when I do I get very strange errors.

Here's what it looks like now:

from django.db import models
from django.contrib.auth.models import User
from projectprofiler.middleware import threadlocals

#CHOICES defined here
#...

class Project(models.Model):
name = models.CharField(max_length=200)
complexity = models.PositiveIntegerField
(choices=COMPLEXITY_CHOICES, default=0)
affiliation = models.PositiveIntegerField
(choices=AFFILIATION_CHOICES, default=0
description = models.TextField(max_length=300,
blank=True) # not required
...etc


...as you can see, everything is coming from django.db.models. I have
never had to specify the actual widgets that input the data in the
admin pages on the site... they are just automatic. What confuses me
to death is how all I need to do is add "from django import forms" to
the above code to make it break.

-Adam

Brian Neal

unread,
Mar 24, 2009, 10:59:02 AM3/24/09
to Django users
I thought you were trying to make a form. Are you trying to do all
this from the admin?
If that is the case you can provide your own form that the admin will
use. In that form is where you specify the fields and widgets you
need.
Take a look at the forms documentation and also the model-forms docs.

Adam Fraser

unread,
Mar 26, 2009, 10:59:46 AM3/26/09
to Django users
hrm, I think I'm _finally_ starting to understand how django is meant
to be used.

Question: Why can't I just specify the widget used by a particular
field from my model in the admin interface.

stains = models.CommaSeparatedIntegerField(widget=SelectMultiple
(choices=STAIN_CHOICES))

...or can I? You suggested something like this earlier only referred
to forms.CommaSeparatedIntegerField rather than
models.CommaSeparatedIntegerField.

hrm

Brian Neal

unread,
Mar 26, 2009, 11:39:49 AM3/26/09
to Django users
On Mar 26, 9:59 am, Adam Fraser <adam.n.fra...@gmail.com> wrote:
> hrm, I think I'm _finally_ starting to understand how django is meant
> to be used.
>
> Question: Why can't I just specify the widget used by a particular
> field from my model in the admin interface.
>
> stains = models.CommaSeparatedIntegerField(widget=SelectMultiple
> (choices=STAIN_CHOICES))
>
> ...or can I?  You suggested something like this earlier only referred
> to forms.CommaSeparatedIntegerField rather than
> models.CommaSeparatedIntegerField.
>
> hrm

Models are simply models. They don't really say much about forms. What
the admin application does is to make some default choices about how
to display your model in the admin interface. It automatically builds
the form to edit your model using some very reasonable defaults. If
you aren't happy with those choices, you can provide the form that the
admin should use instead of the default one. In your form, you are
free to use different widgets for your fields and/or provide custom
behavior.

The docs describe how to provide your own form to the admin here:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#form

You simply provide an attribute on your ModelAdmin called 'form', set
equal to the form class you want the admin to use. This form class
should inherit from ModelForm. You can then customize the widgets and
behavior anyway you would like.

ModelForms are discussed here:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#topics-forms-modelforms

Overriding the default field type for a field on a ModelForm is a bit
further down:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types

But before you read all that, make sure you understand forms in
Django, in general:
http://docs.djangoproject.com/en/dev/topics/forms/#topics-forms-index

Armed with that info, you should be able to make a custom form for the
admin to use. Good luck.

BN

Adam Fraser

unread,
Mar 26, 2009, 2:08:21 PM3/26/09
to Django users
First, I want to thank you for sticking with me and helping me through
this. I've learned a lot, but unfortunately made no progress yet.

I read the links you sent and ended up trying this in forms.py:

from django.contrib import admin
class ProjectAdmin(admin.ModelAdmin):
formfield_overrides = {
models.CommaSeparatedIntegerField: {'widget':
forms.SelectMultiple},
}

but I get an error " 'module' object has no attribute 'ModelAdmin' "
(I verified this from python manage.py shell)

My guess is I'm using an older django distribution. Unfortunately,
this isn't something I can update since it's on a server I have
limited access to.

Before taking this tack, I was trying something like this (again, in
forms.py)
( http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types
)

class ProjectForm(forms.ModelForm):
stains = forms.CommaSeparatedIntegerField
(widget=forms.SelectMultiple(choices=STAINS_CHOICES))
class Meta:
model = Project

...with this I get a strange error:
ViewDoesNotExist at /projectprofiler/admin/projects/project/504/
Tried logout_view in module projectprofiler.projects.views. Error was:
'module' object has no attribute 'CommaSeparatedIntegerField'
Request Method: GET
Request URL: http://imageweb.broad.mit.edu:8081/projectprofiler/admin/projects/project/504/
Exception Type: ViewDoesNotExist
Exception Value: Tried logout_view in module
projectprofiler.projects.views. Error was: 'module' object has no
attribute 'CommaSeparatedIntegerField'
Exception Location: /imaging/analysis/People/imageweb/python-packages/
django/core/urlresolvers.py in _get_callback, line 184
...
In template /home/radon01/afraser/projectprofiler/templates/admin/
base.html, error at line 28
28 <a href="{% url projectprofiler.admin.views.main.password_change
%}">{% trans 'Change password' %}</a>

...I can't figure out what the password_change view as to do with the
commaseparatedintegerfield.

Thanks again :]
Adam
> ModelForms are discussed here:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#topics-...
>
> Overriding the default field type for a field on a ModelForm is a bit
> further down:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overrid...

Adam Fraser

unread,
Apr 6, 2009, 11:57:47 AM4/6/09
to Django users
Does anyone have a clue why I can't access ModelAdmin in
django.contrib.admin?

>>> from django.contrib import admin
>>> dir(admin)
['__builtins__', '__doc__', '__file__', '__name__', '__path__',
'models']

???

I'm trying to do this:

class ProjectAdmin(admin.ModelAdmin):
formfield_overrides = {
models.CommaSeparatedIntegerField: {'widget':
forms.SelectMultiple},
}

admin.site.register(Project, ProjectAdmin)


OR this

class ProjectAdmin(admin.ModelAdmin):
form = ProjectForm

admin.site.register(Project, ProjectAdmin)

class ProjectForm(forms.ModelForm):
class Meta:
model = Project
stains = forms.CommaSeparatedIntegerField
(widget=forms.SelectMultiple(choices=STAINS_CHOICES))

but I can't figure out what I'm doing wrong.

-Adam

On Mar 26, 2:08 pm, Adam Fraser <adam.n.fra...@gmail.com> wrote:
> First, I want to thank you for sticking with me and helping me through
> this.  I've learned a lot, but unfortunately made no progress yet.
>
> I read the links you sent and ended up trying this in forms.py:
>
> from django.contrib import admin
> class ProjectAdmin(admin.ModelAdmin):
>     formfield_overrides = {
>         models.CommaSeparatedIntegerField: {'widget':
> forms.SelectMultiple},
>     }
>
> but I get an error " 'module' object has no attribute 'ModelAdmin' "
> (I verified this from python manage.py shell)
>
> My guess is I'm using an older django distribution.  Unfortunately,
> this isn't something I can update since it's on a server I have
> limited access to.
>
> Before taking this tack, I was trying something like this (again, in
> forms.py)
> (http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overrid...
> )
>
> class ProjectForm(forms.ModelForm):
>     stains = forms.CommaSeparatedIntegerField
> (widget=forms.SelectMultiple(choices=STAINS_CHOICES))
>     class Meta:
>         model = Project
>
> ...with this I get a strange error:
> ViewDoesNotExist at /projectprofiler/admin/projects/project/504/
> Tried logout_view in module projectprofiler.projects.views. Error was:
> 'module' object has no attribute 'CommaSeparatedIntegerField'
> Request Method:         GET
> Request URL:    http://imageweb.broad.mit.edu:8081/projectprofiler/admin/projects/pro...

Karen Tracey

unread,
Apr 6, 2009, 1:32:00 PM4/6/09
to django...@googlegroups.com
On Mon, Apr 6, 2009 at 11:57 AM, Adam Fraser <adam.n...@gmail.com> wrote:

Does anyone have a clue why I can't access ModelAdmin in
django.contrib.admin?

>>> from django.contrib import admin
>>> dir(admin)
['__builtins__', '__doc__', '__file__', '__name__', '__path__',
'models']

???

That's the result I get if my PYTHONPATH is pointing to a 0.96.x version of Django.

Karen

Adam Fraser

unread,
Apr 6, 2009, 1:40:19 PM4/6/09
to Django users
Yeah, I just realized I'm running django 0.97

On Apr 6, 1:32 pm, Karen Tracey <kmtra...@gmail.com> wrote:

Alex Gaynor

unread,
Apr 6, 2009, 1:45:48 PM4/6/09
to django...@googlegroups.com
There is no Django .97, presumably you mean some SVN version after .96.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Adam Fraser

unread,
Apr 7, 2009, 9:47:38 AM4/7/09
to Django users
Yup:
>>> django.get_version()
u'0.97-pre-SVN-unknown'

On Apr 6, 1:45 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:

ram...@gmail.com

unread,
Apr 8, 2009, 8:44:07 AM4/8/09
to Django users
Adam, did you solve you problem?

I try to solve the same problem here
http://stackoverflow.com/questions/723639/forms-selectmultiple-from-models-commaseparatedintegerfield
Reply all
Reply to author
Forward
0 new messages