From 94a1d6d134b64bb750e117f71b32c7a2e3d3c4b3 Mon Sep 17 00:00:00 2001 From: milkaknap Date: Sat, 12 Apr 2014 23:18:29 +0200 Subject: [PATCH 1/2] Update forms.py Add support for default value like in django.db.models.fields.Field. Example: class MyModel(models.Model): flags = BitField(flags=(...), default=('flaggy_foo', 'flaggy_bar')) --- bitfield/forms.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bitfield/forms.py b/bitfield/forms.py index 352a558d..f00e47f0 100644 --- a/bitfield/forms.py +++ b/bitfield/forms.py @@ -41,3 +41,8 @@ def clean(self, value): except AttributeError: raise ValidationError('Unknown choice: %r' % (k,)) return int(result) + + def prepare_value(self, value): + if not value: + return [] + return BitHandler(value, [k for k, v in self.choices]) From 1e4e87fedead7aab03d83ef4fc90a42d445e2a79 Mon Sep 17 00:00:00 2001 From: milkaknap Date: Sat, 10 May 2014 11:49:40 +0200 Subject: [PATCH 2/2] Update forms.py Correct indentation and bugfix (if value is tuple or list, it cannot be processed by BitHandler). --- bitfield/forms.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bitfield/forms.py b/bitfield/forms.py index f00e47f0..a1f4bbc6 100644 --- a/bitfield/forms.py +++ b/bitfield/forms.py @@ -43,6 +43,9 @@ def clean(self, value): return int(result) def prepare_value(self, value): - if not value: - return [] - return BitHandler(value, [k for k, v in self.choices]) + if not value: + return [] + try: + return BitHandler(value, [k for k, v in self.choices]) + except TypeError: + return value