So I am heading down the path of creating a MultiWidget with a Select
drop down list and a radio field displayed side by side.  But the
problem that I am currently encountering into is that my compress
method in the MultiValueField is not being called.  The odd thing is
that if I change the Select field to something other like a
CheckboxInput field the compress method IS called.

Any ideas what is wrong with the code below and why my compress method
is not being called?


# Asset Number Field
UPDATE_CHILDREN_CHOICES = (
    ('False', 'Update only this device'),
    ('True', 'Update all children of this device'))

class AssetNumberWidget(forms.MultiWidget):
    def __init__(self, attrs=None):
        asset_choices = Asset.objects.values_list('idAsset',
'Asset_Number')
        widgets = (forms.Select(attrs=attrs, choices=asset_choices),
                   forms.RadioSelect(choices=UPDATE_CHILDREN_CHOICES,
renderer=CustomRadioFieldRenderer, attrs=attrs))
        super(AssetNumberWidget, self).__init__(widgets, attrs)

    def decompress(self, value):
        return [value, 'False']

class AssetNumberField(forms.MultiValueField):
    default_error_messages = {
        'invalid_asset_number': u'Enter a valid asset number.',
        'invalid_update_preference': u'Enter a valid selection to
update only this device or to also update all children devices.',
    }

    def __init__(self, *args, **kwargs):
        fields = (
            forms.ChoiceField(label='Asset Number'),
            forms.ChoiceField(choices=UPDATE_CHILDREN_CHOICES))
        widgets = AssetNumberWidget()

        if not 'widget' in kwargs:
            kwargs['widget'] = AssetNumberWidget()
        if not 'initial' in kwargs:
            kwargs['initial'] = 'False'
        super(AssetNumberField, self).__init__(fields, *args,
**kwargs)

    def compress(self, data_list):
        if data_list:
            if data_list[0] in EMPTY_VALUES:
                raise ValidationError(self.error_messages
['invalid_asset_number'])
            if data_list[1] in EMPTY_VALUES:
                raise ValidationError(self.error_messages
['invalid_update_preference'])
            return data_list
        return None

On Jan 13, 4:31 pm, SnappyDjangoUser <bpwall...@gmail.com> wrote:
> Hi All,
>
> Is there a way to "link" 2 form fields or use a create a MultiWidget
> consisting of 2 fields so they are logically displayed together?  I
> have a search form which asks the user for a date range to search and
> I am trying to figure out the best way to display them.  The obvious
> option would be to make two separate form fields, a start_date and
> end_date, but I don't like that option. I would much rather have both
> selects show up on the same line, which is not the behavior you get
> with two separate fields.
>
> I am aware of how to do this with a MultiWidget and a MultiValueField,
> but a MultiValueField requires that you compress the data into a
> single value.  I want to return both values, the start_date and
> end_date of the date range, to the view for searching.
>
> Thanks for your input!
>
> -Brian
--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to