Answering my own question:

The widget can be declared as part of the field, e.g.

# Field
class MoneyField(MultiValueField):
    def __init__(self, currency=(), amount=(), required=True,
widget=None, label=None, initial=None):
        widget  = widget or MoneyWidget(currency=currency,
amount=amount)
.....

Or  something like that.

On Jan 24, 8:36 pm, "canen" <[EMAIL PROTECTED]> wrote:
> Hello All,
>
> I am resending this -- seems it didn't reach the last time, sorry if it
> turns up twice
> ------------
>
> I've been messing around with the new MultiValueField and MultiWidget.
> I don't  know if the shortcoming is with me or with the implementation
> of the field and widget. Let's say I want to create a field that
> displays two select fields that are related, I'll use currency display
> as an example. The resulting HTML should look like:
>
> <select name="budget_0" id="id_budget">
> <option value="USD">USD</option>
> <option value="EU">EU</option>
> </select>
> <select name="budget_1" id="id_budget">
> <option value="1000">1000</option>
> <option value="2000">2000</option>
> </select>
>
> and returns a value like "USD 1000". The implementation I have so far
> is:
>
> # Field
> class MoneyField(MultiValueField):
>     def __init__(self, currency=(), amount=(), required=True,
> widget=None, label=None, initial=None):
>         fields = (ChoiceField(choices=currency),
> ChoiceField(choices=amount))
>         super(MoneyField, self).__init__(fields, required, widget,
> label, initial)
>
>     def compress(self, data_list):
>         if data_list:
>             return " ".join(i for i in data_list)
>         return None
>
> # Widget
> class MoneyWidget(MultiWidget):
>     def __init__(self, attrs=None, currency=(), amount=()):
>         widgets = (Select(attrs=attrs, choices=currency),
> Select(attrs=attrs, choices=amount))
>         super(MoneyWidget, self).__init__(widgets, attrs)
>
>     def decompress(self, value):
>         if value:
>             return value.split(' ', 1)
>         return ['', '']
>
> The problem is,  when I use the field I have to repeat the choices for
> both  widget and the field, like so:
>
> CURRENCY_CHOICES = [('USD', 'USD'), ('EU', 'EU')]
> AMOUNT_CHOICES = [('1000', '1000'), ('2000', '2000')]
>
> class MyForm(forms.Form):
>     budget = MoneyField(
>                 label="Vacation budget (hotel amount only)",
>                 currency=CURRENCY_CHOICES,
>                 amount=AMOUNT_CHOICES,
>                 required=False,
>                 widget=MoneyWidget(currency=CURRENCY_CHOICES,
> amount=AMOUNT_CHOICES)
>             )
>
> Am I doing something wrong here are is it just a caveat with using
> ChoiceField? Any guidance would be appreciated.
> 
> Cheers!


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to