Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django 1.4.2 admin #28

Open
zaxl opened this issue Dec 6, 2012 · 9 comments
Open

django 1.4.2 admin #28

zaxl opened this issue Dec 6, 2012 · 9 comments

Comments

@zaxl
Copy link

zaxl commented Dec 6, 2012

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

@zaxl
Copy link
Author

zaxl commented Dec 6, 2012

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.

@emersonmx
Copy link

dude, the formatting is not helping.

@zaxl
Copy link
Author

zaxl commented Feb 28, 2013

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.

@emersonmx
Copy link

Working! 👍 thanks

@jobelenus
Copy link

This works great. Can we make a pull request for this?

@zaxl
Copy link
Author

zaxl commented Jun 10, 2013

Here it is : #32

@zxkane
Copy link

zxkane commented Nov 9, 2013

The usage of admin in README also has typo,

It should be,

    list_filter = (
            ('flags', BitFieldListFilter),
            )

@LukeSkyw
Copy link

Hi, can a maintainer merge this request. I have a similar problem. Impossible to add en entry which has a bitfield.

@SeanHayes
Copy link
Contributor

Related PRs: #58 and #63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants