Skip to content

madisona/django-multiselect

Repository files navigation

This project provides a multiselect widget for Django.

We're using https://github.com/michael/multiselect for the widget itself and we started with http://djangosnippets.org/snippets/2079/ but added a few more things including:

  • Integration with Django's admin site
  • A model field that will use the multiselect widget by default
  • A form field that will use the multiselect widget by default

HOW TO INSTALL SAMPLE APPLICATION:

  1. Download the source
  2. Run python bootstrap.py -d
Note:
  • The -d command tells bootstrap to use distribute, easy_install's successor
  • If you want to run your project with a different version of python than your default, specify that when you run the bootstrap command. IE: python2.4 bootstrap.py -d
  1. Run bin/buildout Note: This will get all your project's dependencies listed in your setup.py file.
  2. You'll want to move the multiselect/media folder and its contents to a place your static media url can see. Be sure to update widgets.py to point to the right directory.

HOW TO ADD TO YOUR APPLICATION:

  • Buildout

    We highly recommend using Buildout. If you are using buildout you can add the following to your setup.py:

    dependency_links = (
            'http://github.com/mattjmorrison/django-multiselect/tarball/1.0#egg=django-multiselect',
    ),
    

    and add 'django-multiselect' to install_requires

  • Distutils

    If you prefer to install modules at a system level download the source and run:

    python setup.py install
    

HOW TO USE AS A FIELD:

  • In Your Models:
    Use multiselect.fields.ManyToManyField instead of django.db.models.ManyToManyField
  • In Your Forms:
    Use multiselect.fields.MultipleChoiceField instead of django.forms.MultipleChoiceField
  • If you want to just use the widget:
    set widget=multiselect.widgets.MultiSelect() on your form's field

DEPENDENCIES:

The following will need to be manually added to your template(s) so they are available to the widget

USING THE MULTISELECT WIDGET IN DJANGO'S ADMIN SITE:

The admin site needs the same dependencies, here is how we added them. We created our own admin/base_site.html (make sure it is in your template_dirs in settings.py) You can either copy Django's admin/base_site.html as a starting point, our use ours.

Just add the following:

{% block extrahead %}
    <link type="text/css" rel="stylesheet" href="{{ MEDIA_URL }}css/themes/smoothness/jquery-ui-1.7.1.custom.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>

    <style type="text/css">
        form .ui-multiselect {margin-right: 5px; float: left; border-color: #ccc;}
        form .ui-multiselect div.selected {border-color: #ccc;}
    </style>
{% endblock %}

This will add jquery, jquery-ui, a jquery-ui theme and some css so the multiselect widget will play nicely with the Django admin site's styles

Note: We disabled some of the options like draggable and sortable because they didn't seem to work well and I didn't like the animations anyways.

About

using django with michael's multiselect jquery plugin

Resources

License

Stars

Watchers

Forks

Packages

No packages published