AndrewIngram / django-extra-views

Django's class-based generic views are awesome, let's have more of them.
MIT License
1.39k stars 172 forks source link

Using widgets with UpdateWithInlinesView #243

Closed typonaut closed 2 years ago

typonaut commented 2 years ago

Could someone kindly point me in the right direction for using widgets with UpdateWithInlinesView?

I have tried the following:

class devMemberRegForm(InlineFormSetFactory):
    model = Member_Register
    fields = ['member', 'present', 'late']

    present = forms.BooleanField(label=_('present'), required=False, initial=True, widget=forms.CheckboxInput)

    #or

    class Meta:
        model = Member_Register

        fields = (
            'member',
            'present',
            'late',
            )

        widgets = {
            'present': forms.CheckboxInput,
            …
        }

class devRegisterView(UpdateWithInlinesView):
    model = Register
    inlines = [devMemberRegForm]
    …

Thank you

typonaut commented 2 years ago

Ok, worked it out – I was thinking that InlineFormSetFactory was a form, when in fact it is a view:

views.py

class devMemberReg(InlineFormSetFactory):
    # This is a VIEW, not a FORM
    model = Member_Register
    form_class = devControlRegisterLineForm # form for "inline/child"
    factory_kwargs = {'extra': 1, 'max_num': 100,
                      'can_order': False, 'can_delete': False}

class devRegisterView(UpdateWithInlinesView):
    model = Register
    inlines = [devMemberReg] #InlineFormSetFactory immediately above
    template_name = 'manage/dev/register_edit.html'
    form_class = ControlRegisterForm # form for "parent"

    def get_success_url(self):
        return …

This way the widgets on the form_class devControlRegisterLineForm behave as expected.