Not sure exactly what the issue is, but this may be handy (either as a 
replacement or as an example of doing something somewhat similar):
https://github.com/disqus/django-bitfield

I believe on most machines the maximum is 63 "flags" though so you will 
likely only be able to use it as an example of doing something very 
similar, unless you split it into 7 days of 24 hours which may be more 
database efficient (7 BigInts is probably easier for a database to 
store/query than a 168 char string). One interesting trick for going this 
route is you could query for if one of the days is > 0 and if it is at 
least one item is checked instead of having to do string comparisons.

On Friday, April 5, 2013 4:07:02 AM UTC-7, Дмитрий Кольдяев wrote:
>
> I`m trying to create form field for Django admin backend In database it 
> should be CharField field, contains "0" and "1" values with 24*7 length. In 
> admin panels I want to see them as 7 fields with 24 checkboxes each. If 
> checkbox is checked coressponding position of the string sets to "1", else 
> "0".
>
> model.py
>
> from django.db import modelsfrom places.widgets import *from places.fields 
> import *
> class Place(models.Model):
>     …
>     time =          HoursWorkByWeekField()
>     …
>
> fields.py
>
> from django.forms import fieldsfrom django.db import models
> from places.widgets import HoursWorkByWeekWidget
> class HoursWorkByWeekField(models.CharField):
>     widget = HoursWorkByWeekWidget
>
>     def __init__(self):
>         super(HoursWorkByWeekField, self).__init__(max_length=24*7, 
> min_length=24*7)
>
>     def to_python(self, value):
>         return list(value)
>
>     def get_db_prep_value(self, value):
>         result = []
>         for i in range(24*7):
>             if value[i] :
>                 result += ['1']
>             else :
>                 result += ['0']
>         return ''.join(result)
>
> widgets.py
>
> from django.forms import widgets
> class HoursWorkByDayWidget(widgets.CheckboxSelectMultiple):
>     def render(self, name, attrs=None):
>         choices = range(0,24,1)
>         return widgets.CheckboxSelectMultiple.render(self, name, attrs, 
> choices)
> class HoursWorkByWeekWidget(widgets.MultiWidget):
>     def __init__(self, attrs=None):
>         widgets = (
>             HoursWorkByDayWidget(),
>             HoursWorkByDayWidget(),
>             HoursWorkByDayWidget(),
>             HoursWorkByDayWidget(),
>             HoursWorkByDayWidget(),
>             HoursWorkByDayWidget(),
>             HoursWorkByDayWidget(),
>         )
>         super(HoursWorkByWeekWidget, self).__init__(widgets, attrs)
>
> When i try syncdb i have error:
>
> TypeError: Error when calling the metaclass bases unhashable type: 'dict'
>
> What am I doing wrong? How to organize code?
> Many thanks in advance
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to