简体   繁体   中英

How to hide all labels from modelformset_factory generated inputs?

I have following code in forms.py

class MCQuestionForm(forms.ModelForm):

    class Meta:
        model = models.MultipleChoiceQuestion
        fields = ('prompt',)

    def __init__(self, *args, **kwargs):
        super(MCQuestionForm, self).__init__(*args, **kwargs)        
        self.fields['choice'] = forms.ModelChoiceField(queryset=self.instance.choice.all(),
                                                   widget=forms.RadioSelect,
                                                   empty_label=None)


MCQuestionFormSetForUser = modelformset_factory(models.MultipleChoiceQuestion,
                                            fields=('prompt',),
                                            form=MCQuestionForm,
                                            extra=0,
                                            widgets={
                                                'prompt': forms.TextInput(
                                                    attrs={'readonly': True, 'class': 'borderless'})
                                            }
                                            )

When I place "MCQuestionFormSetForUser" as formset.as_p in a template both prompt field and choices get corresponding labels. How can I hide them?

Found the way to do that

class MCQuestionForm(forms.ModelForm):

    class Meta:
        model = models.MultipleChoiceQuestion
        fields = ('prompt',)

        def __init__(self, *args, **kwargs):
            super(MCQuestionForm, self).__init__(*args, **kwargs)
            self.fields['choice'] = forms.ModelChoiceField(queryset=self.instance.choice.all(),
                                                   widget=forms.RadioSelect,
                                                   empty_label=None)

            # here is the override that I was looking for
            self.fields['choice'].label = ''
            self.fields['prompt'].label = ''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM