Login

Multiple Choice model field

Author:
danielroseman
Posted:
November 18, 2008
Language:
Python
Version:
1.0
Score:
6 (after 8 ratings)

Usually you want to store multiple choices as a manytomany link to another table. Sometimes however it is useful to store them in the model itself. This field implements a model field and an accompanying formfield to store multiple choices as a comma-separated list of values, using the normal CHOICES attribute.

You'll need to set maxlength long enough to cope with the maximum number of choices, plus a comma for each.

The normal get_FOO_display() method returns a comma-delimited string of the expanded values of the selected choices.

The formfield takes an optional max_choices parameter to validate a maximum number of choices.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from django.db import models
from django import forms

class MultiSelectFormField(forms.MultipleChoiceField):
    widget = forms.CheckboxSelectMultiple
    
    def __init__(self, *args, **kwargs):
        self.max_choices = kwargs.pop('max_choices', 0)
        super(MultiSelectFormField, self).__init__(*args, **kwargs)

    def clean(self, value):
        if not value and self.required:
            raise forms.ValidationError(self.error_messages['required'])
        if value and self.max_choices and len(value) > self.max_choices:
            raise forms.ValidationError('You must select a maximum of %s choice%s.'
                    % (apnumber(self.max_choices), pluralize(self.max_choices)))
        return value

class MultiSelectField(models.Field):
    __metaclass__ = models.SubfieldBase

    def get_internal_type(self):
        return "CharField"

    def get_choices_default(self):
        return self.get_choices(include_blank=False)

    def _get_FIELD_display(self, field):
        value = getattr(self, field.attname)
        choicedict = dict(field.choices)

    def formfield(self, **kwargs):
        # don't call super, as that overrides default widget if it has choices
        defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 
                    'help_text': self.help_text, 'choices':self.choices}
        if self.has_default():
            defaults['initial'] = self.get_default()
        defaults.update(kwargs)
        return MultiSelectFormField(**defaults)

    def get_db_prep_value(self, value):
        if isinstance(value, basestring):
            return value
        elif isinstance(value, list):
            return ",".join(value)

    def to_python(self, value):
        if isinstance(value, list):
            return value
        return value.split(",")

    def contribute_to_class(self, cls, name):
        super(MultiSelectField, self).contribute_to_class(cls, name)
        if self.choices:
            func = lambda self, fieldname = name, choicedict = dict(self.choices):",".join([choicedict.get(value,value) for value in getattr(self,fieldname)])
            setattr(cls, 'get_%s_display' % self.name, func)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

codekoala (on February 24, 2009):

Thank you for this!

#

chester (on May 20, 2009):

Hi! Did you try to dumpdata using that field ? I had to change it a bit to work good with dumping and loading data:

added new method to MultiSelectField class:

def value_to_string(self, obj):
    value = self._get_val_from_obj(obj)
    return self.get_db_prep_value(value)

and fixed this one:

def to_python(self, value):
    if isinstance(value, list):
        return value
    elif value==None:
        return ''
    return value.split(",")

Before changes the field was serializing raw data in form:

{ data : "[u'1', u'2', u'5']" }

instead of just

{ data: "1,2,5" }

I'm new to django, maybe it's not a bug or maybe I was doing this wrong, but the important thing is that now it works fine for me. Hope it will help someone.

Anyway, thanks for such great field! :)

#

jenlu (on June 9, 2010):

I have had this code running on Django 1.1 but when upgrading to 1.2.1 it stops. Keep geeting that:

Value ['a', 'b', 'c'] is not a valid choice.

Any hints to why?

#

Davepar (on June 21, 2010):

So you don't have to read the entire thread, for Django 1.2 you need to add a simple, empty validate function:

def validate(self, value, model_instance): 
    return

I haven't personally tried this yet, so not sure if it's on the form field or model field.

#

Davepar (on June 21, 2010):

To save everybody one other minor error, include this at the top:

from django.utils.text import capfirst

#

npdoty (on July 31, 2011):

chester's fix for handling value==None is also required to get this to work with creating a new item via the admin interface.

#

pabdelhay (on October 19, 2011):

Daniel, This snippet was very useful for me! Thank you. And Davepar... you saved my day with your tip.

Just to avoid inconsistent data in DB, I put some code in the validation method to do the validation as it should.

def validate(self, value, model_instance):
    arr_choices = self.get_choices_selected(self.get_choices_default())
    for opt_select in value:
        if (opt_select not in arr_choices): 
            raise exceptions.ValidationError(self.error_messages['invalid_choice'] % value)    
    return

def get_choices_selected(self, arr_choices=''):
    if not arr_choices:
        return False
    list = []
    for choice_selected in arr_choices:
        list.append(choice_selected[0])
    return list

#

pabdelhay (on October 19, 2011):

This code above goes inside the model field.

#

airtonix (on January 15, 2012):

paddelhay, you forgot to mention that :

from django.core import exceptions

is required.

#

danielfeng (on February 18, 2012):

Not good:

elif value==None:

Should be:

def to_python(self, value):
    if value is not None:
        return value if isinstance(value, list) else value.split(',')
    return ''

#

danielfeng (on February 18, 2012):

Also the get_db_prep_value() has changed since Django 1.2, so change:

def get_db_prep_value(self, value):

To:

def get_db_prep_value(self, value, connection, prepared=False):

#

pCoelho (on March 4, 2012):

Is it possible to also have option groups?

Example:

x = ( ('outer1', 'Outer 1'), ('Group 1', ( ('inner1', 'Inner 1'), ('inner2', 'Inner 2'), ) ) )

I tried it but it's not displaying correctly...

#

mhost2 (on March 18, 2012):

In order for this to work with south, add the following at the top of the file you put this code in (change path to match):

from south.modelsinspector import add_introspection_rules  
add_introspection_rules([], ["^appname\.fields\.MultiSelectField"])

#

mhost2 (on March 19, 2012):

As noted by danielfeng, since Django 1.2, get_db_prep_value expects a connection. Therefore chester's value_to_string method should pass a connection. Otherwise, when doing a dumpdata, you will get:

DeprecationWarning: get_db_prep_value has been called without providing a connection argument.

From my understanding, get_db_prep_value is meant for database backend specific preparations. This field's database preparations are very generic and therefore should be changed to get_prep_value. This solves the deprecation warning. Here are the 2 updated methods:

def get_prep_value(self, value):
    if isinstance(value, basestring):
        return value
    elif isinstance(value, list):
        return ",".join(value)

def value_to_string(self, obj):
    value = self._get_val_from_obj(obj)
    return self.get_prep_value(value)

Note that there is no get_db_prep_value method anymore.

#

Please login first before commenting.