From 58d217fe370645d2af132601f4410bfd25d354ee Mon Sep 17 00:00:00 2001 From: habfast Date: Mon, 24 Feb 2014 22:33:22 +0100 Subject: [PATCH] fix BitFieldCheckboxSelectMultiple for new form In the admin, when creating a new form, the default is the integer version of the value, which cannot be looped over in CheckboxSelectMultiple.render() (raises an Error). This converts the integer value to Array. Works with default=X, X being an integer or an array of flags. --- bitfield/forms.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bitfield/forms.py b/bitfield/forms.py index 352a558..bbb9031 100644 --- a/bitfield/forms.py +++ b/bitfield/forms.py @@ -8,6 +8,15 @@ class BitFieldCheckboxSelectMultiple(CheckboxSelectMultiple): def render(self, name, value, attrs=None, choices=()): if isinstance(value, BitHandler): value = [k for k, v in value if v] + elif isinstance(value, int): + real_value=[] + div=2 + for (k,v) in self.choices: + if value % div != 0: + real_value.append(k) + value -= (value % div) + div*=2 + value=real_value return super(BitFieldCheckboxSelectMultiple, self).render( name, value, attrs=attrs, choices=enumerate(choices))