Thanks for the pointers. I soon came to realise that I did indeed need to
subclass the MultValue classes, and currently have something like this:
class PostcodeField(forms.MultiValueField):
def compress(self, data_list):
return ''.join(data_list)
class PostcodeWidget(forms.MultiWidget):
def __init__(self, attrs=None):
widgets = (forms.TextInput(attrs=attrs), forms.TextInput(attrs=attrs))
super(PostcodeWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return value.split('__')
return ['', '']
def format_output(self, rendered_widgets):
return u''.join(rendered_widgets)
Then in the form class:
postcode = PostcodeField(
fields = (
forms.CharField(max_length = 4),
forms.CharField(max_length = 4)
),
widget = PostcodeWidget
)
Far from perfect yet, but I understand a lot more about how this code
functions.
-Phil
On 06/02/07, canen <[EMAIL PROTECTED]> wrote:
>
>
> Phil,
>
> Thought an example might help.
>
> class VacationBudgetField(MultiValueField):
> def __init__(self, required=True, label=None, widget=None,
> initial=None, f_type=(), currency=(), amount=None):
> fields = (ChoiceField(choices=f_type),
> ChoiceField(choices=currency), IntegerField())
> widget = widget or BudgetWidget(f_type=f_type,
> currency=currency, amount=amount)
> super(VacationBudgetField, self).__init__(fields, required,
> widget, label, initial)
>
> def compress(self, data_list):
> if data_list:
> return " ".join(str(i) for i in data_list)
> return None
>
> ## Where BuggetWidget is
> class BudgetWidget(MultiWidget):
> def __init__(self, attrs=None, f_type=(), currency=(),
> amount=None):
> widgets = (Select(attrs=attrs, choices=f_type),
> Select(attrs=attrs, choices=currency), TextInput())
> super(BudgetWidget, self).__init__(widgets, attrs)
>
> def decompress(self, value):
> if value:
> return value.split(' ', 2)
> return ['', '', '']
>
> ## Used it like so...
> budget = VacationBudgetField(f_type=[('a', 'a'), ('b', 'b')],
> currency=[('d', 'd'), ('e', 'e')])
>
> Hope that helps.
>
> On Feb 6, 4:51 pm, "canen" <[EMAIL PROTECTED]> wrote:
> > Phil,
> >
> > I think you must subclass it and define the compress method. See the
> > comments herehttp://code.djangoproject.com/browser/django/trunk/
> > django/newforms/fields.py#L412. You will also need a MultiWidget to
> > go with the field.
> >
> > On Feb 6, 10:04 am, "Phil Powell" <[EMAIL PROTECTED]> wrote:
> >
> > > Hi,
> >
> > > Can anyone provide any pointers on how I use MultiValueField? Can I
> > > use it straight out of the box, or do I have to subclass to create a
> > > new field and a new MultiValueWidget?
> >
> > > I thought that this code would just work:
> >
> > > postcode = forms.MultiValueField(fields=(forms.CharField
> (max_length=4),
> > > forms.CharField(max_length=4)))
> >
> > > But all I ever get rendered in my template (using {{ form.postcode }})
> > > is a single text input.
> >
> > > Any advice would be much appreciated.
> >
> > > -Phil
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---