django 1.4.2 admin
zaxl opened this issue · 9 comments
Exception Type: TypeError at /admin/article/sarticle/add/
Exception Value: 'int' object is not iterable
local vars:
name
'genre_flags'
self
<bitfield.forms.BitFieldCheckboxSelectMultiple object at 0xb40832ac>
attrs
{'id': u'id_genre_flags'}
value
0
choices
()
model:
genre_flags = BitField(flags=[
'PressRelease',
'Satire',
'Blog',
'OpEd',
'Opinion',
'UserGenerated',
], default=('PressRelease',), help_text=genres_help)
admin.py:
formfield_overrides = {
BitField: {'widget': BitFieldCheckboxSelectMultiple},
}
When editing the widget works as expected.
The exception occurs only when adding a new object.
fix follows
diff --git a/bitfield/forms.py b/bitfield/forms.py
index f9a8fe6..aa89bdf 100644
--- a/bitfield/forms.py
+++ b/bitfield/forms.py
@@ -25,6 +25,13 @@ class BitFieldCheckboxSelectMultiple(CheckboxSelectMultiple):
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<<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
this seems to fix it. convert initial value that comes as int to a list of flag names.
dude, the formatting is not helping.
django=1.4.2
in the model:
genre_flags = BitField(verbose_name=u'Genre',flags=[
'PressRelease',
'Satire',
'Blog',
'OpEd',
'Opinion',
'UserGenerated',
], default=('PressRelease',),help_text=genres_help)
does the job for me.
Working! 👍 thanks
This works great. Can we make a pull request for this?
The usage of admin in README also has typo,
It should be,
list_filter = (
('flags', BitFieldListFilter),
)
Hi, can a maintainer merge this request. I have a similar problem. Impossible to add en entry which has a bitfield.