diff --git a/bitfield/forms.py b/bitfield/forms.py index f9a8fe6..2d27225 100644 --- a/bitfield/forms.py +++ b/bitfield/forms.py @@ -25,6 +25,13 @@ def _has_changed(self, initial, data): class BitFormField(IntegerField): def __init__(self, choices=(), widget=BitFieldCheckboxSelectMultiple, *args, **kwargs): + iv = kwargs.get('initial') + if isinstance(iv, int): + l = [] + for i in xrange(0, 63): + if (1 << i) & iv > 0: + l += [choices[i][0]] + kwargs['initial'] = l self.widget = widget super(BitFormField, self).__init__(widget=widget, *args, **kwargs) self.choices = self.widget.choices = choices