From 129d0f584f3f0a35ef829b8f706953d0dc86c89f Mon Sep 17 00:00:00 2001 From: Alexander Atanasov Date: Mon, 10 Jun 2013 13:49:26 +0300 Subject: [PATCH 1/4] Convert initial value that comes as int to a list of flag names. When editing an object the widget works as expected. The exception occurs only when adding a new object. --- bitfield/forms.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bitfield/forms.py b/bitfield/forms.py index f9a8fe6e..dc4f7761 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): + if isinstance(kwargs['initial'],int): + iv = kwargs['initial'] + l = [] + for i in range(0, 63): + if (1< 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 From f81909eab0528736afc58be27c98a83408be8ea6 Mon Sep 17 00:00:00 2001 From: Alexander Atanasov Date: Mon, 10 Jun 2013 13:56:04 +0300 Subject: [PATCH 2/4] fix indent --- bitfield/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitfield/forms.py b/bitfield/forms.py index dc4f7761..b95d82ec 100644 --- a/bitfield/forms.py +++ b/bitfield/forms.py @@ -26,7 +26,7 @@ def _has_changed(self, initial, data): class BitFormField(IntegerField): def __init__(self, choices=(), widget=BitFieldCheckboxSelectMultiple, *args, **kwargs): if isinstance(kwargs['initial'],int): - iv = kwargs['initial'] + iv = kwargs['initial'] l = [] for i in range(0, 63): if (1< 0: From d8f7a8c6c471d374eb05e85259324890bf7660aa Mon Sep 17 00:00:00 2001 From: Alexander Atanasov Date: Mon, 10 Jun 2013 14:00:11 +0300 Subject: [PATCH 3/4] fix indent take two --- bitfield/forms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitfield/forms.py b/bitfield/forms.py index b95d82ec..40ecd384 100644 --- a/bitfield/forms.py +++ b/bitfield/forms.py @@ -25,7 +25,7 @@ def _has_changed(self, initial, data): class BitFormField(IntegerField): def __init__(self, choices=(), widget=BitFieldCheckboxSelectMultiple, *args, **kwargs): - if isinstance(kwargs['initial'],int): + if isinstance(kwargs['initial'],int): iv = kwargs['initial'] l = [] for i in range(0, 63): From 7a6ca66c0e7925a1fe7c5a37740d23e3bf7d8bc1 Mon Sep 17 00:00:00 2001 From: Alexander Atanasov Date: Mon, 10 Jun 2013 22:30:11 +0300 Subject: [PATCH 4/4] Style cleanup. Check if initial is in kwargs. --- bitfield/forms.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bitfield/forms.py b/bitfield/forms.py index 40ecd384..2d27225a 100644 --- a/bitfield/forms.py +++ b/bitfield/forms.py @@ -25,12 +25,12 @@ def _has_changed(self, initial, data): class BitFormField(IntegerField): def __init__(self, choices=(), widget=BitFieldCheckboxSelectMultiple, *args, **kwargs): - if isinstance(kwargs['initial'],int): - iv = kwargs['initial'] + iv = kwargs.get('initial') + if isinstance(iv, int): l = [] - for i in range(0, 63): - if (1< 0: - l += [choices[i][0]] + 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)