Hi all, I figured out how it works.

Using MultiWidget requires three components: MultiWidget, MultiValueField 
and a form.

Here is my example:

class ComplexMultiWidget(forms.MultiWidget):
    def __init__(self, attrs=None):
        widgets = (
            forms.DateInput(),
            forms.Select(choices=(('D', 'Days'),
                                  ('W', 'Weeks'),
                                  ('Y', 'Years'))),
        )
        super(ComplexMultiWidget, self).__init__(widgets, attrs)

    def decompress(self, value):
        if value:
            data = value.split(',')
            return [data[0], data[1]]
        return [None, None, None]

    def format_output(self, rendered_widgets):
        return u'\n'.join(rendered_widgets)


class ComplexField(forms.MultiValueField):
    def __init__(self, required=True, widget=None, label=None, initial=None):
        fields = (
            forms.DateField(widget=forms.DateInput),
            forms.ChoiceField(choices=(('D', 'Days'),
                                       ('W', 'Weeks'),
                                       ('Y', 'Years'))),
        )
        super(ComplexField, self).__init__(fields, required,
                                           widget, label, initial)

    def compress(self, data_list):
        if data_list:
            return '%s,%s' % (data_list[0],''.join(data_list[1]))
        return None

class ComplexFieldForm(forms.Form):
    field1 = ComplexField(widget=ComplexMultiWidget())


I took the solution from here: http://stackoverflow.com/a/2387330

I hope this help someone in the future!



On Monday, December 19, 2016 at 12:40:12 PM UTC-5, Farhan Khan wrote:
>
> Hi all!
> I am trying to get a MultiWidget with two TextFields ,however I am not 
> able to get anything to render. Here is my code.
>
> from django import forms
> from django.forms import widgets
>
> class DateSelectorWidget(widgets.MultiWidget):
>     def __init__(self, attrs=None):
>         _widgets = (forms.TextInput(),
>                     forms.TextInput())
>         super(DateSelectorWidget, self).__init__(_widgets, attrs)
>
>     def format_output(self, rendered_widgets):
>         return ''.join(rendered_widgets)
>
> class Fourteen(forms.Form):
>     mywidget = DateSelectorWidget()
>
>
>
> In the shell I will do:
>
> >>> fourteen = Fourteen()
> >>> print(fourteen)
>
> >>>
>
> Nothing will render. I suspect that I need to manually render the HTML, 
> but the documentation 
> <https://docs.djangoproject.com/en/1.10/ref/forms/widgets/#multiwidget> 
> is fairly light on this topic.
>
> Any assistance would be greatly appreciated!
>

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/548457cb-eeed-4cca-830c-cd7847b9f665%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to