Greetings, I'm having trouble using a Field that created in the Admin.
Here is the Field and the Form.

class BitsField(fields.Field):
    def get_manipulator_field_objs(self):
        return [CheckboxSelectMultipleBitsField]

    def get_internal_type(self):
        return "IntegerField"

class
CheckboxSelectMultipleBitsField(forms.CheckboxSelectMultipleField):
    def prepare(self, new_data):
        print 'prepare'
        # new_data has "split" this field into several fields, so
flatten it
        # back into a single list.
        data_value = 0
        for value, readable_value in self.choices:
            if new_data.get('%s%s' % (self.field_name, value), '') ==
'on':
                data_value += int(value)
        new_data.setlist(self.field_name, [data_value])

    def render(self, data):
        print 'render', data
        output = ['<ul%s>' % (self.ul_class and ' class="%s"' %\
             self.ul_class or '')]
        #Convert data int to list of bit values
        # From
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/219300;
        # Thanks Antti!
        int2bin = lambda n: n>0 and str(n&1)+int2bin(n>>1) or ''
        bit_list = map(int, int2bin(data))
        # Pad the list with appending zeros
        bit_list.append([0] * (len(self.choices) - len(bit_list)))

        index = 0
        for value, choice in self.choices:
            checked_html = ''
            if bit_list[index]:
                checked_html = ' checked="checked"'
            field_name = '%s%s' % (self.field_name, value)
            output.append('<li><input type="checkbox" id="%s"
class="v%s" \
                name="%s"%s /> <label for="%s">%s</label></li>' % \
                (self.get_id() + value , self.__class__.__name__,
field_name,
                    checked_html,self.get_id() + value, choice))
            index += 1
        output.append('</ul>')
        return '\n'.join(output)

When I use them with a manipulator everything works! The form is
generated correctly and all the data saves etc. However, if I include
the same field in my model ala

class Example(models.Model):
    color_choices = (('1', 'red'), ('2', 'blue'), ('3', 'green'))
    colors = BitsField(choices=color_choices)

The admin form is a select box. Not even multi-select, just 'choose one
from the list please'. Some debugging (i.e. print statements)
demonstrates that the render method of my field is not even called.
Further debugging showed that BitsField.get_manipulator_field_obj is
not being called.

Does any one know why the admin is choosing it own field? Is there must
be something in the django source that treats fields with 'choices' in
a special case?

Thanks
--
matthew


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to