1

I am using modelformset_factory.

how do I add a placeholder text in each form field generated?

for example, I have

AdvisorsFormSet = modelformset_factory( S_Advisors, max_num=4, extra=4, exclude=('startup', 'id'))

then

    advisors_item_formset = AdvisorsFormSet(
        request.POST, request.FILES, prefix='advisors',
        queryset=S_Advisors.objects.filter(startup=startup))

how do I add a placeholder text in each form field generated when I render the formset forms in the template?

thanks!

4

1 回答 1

1

modelformset_factory 采用form关键字参数。

所以如果你创建一个 ModelForm

class AdvisorForm( forms.ModelForm ):
  class Meta:
    model=Advisor

  def __init__( self, *args, **kwargs ):
    super( AdvisorForm, self ).__init__( *args, **kwargs )
    self.fields[ 'name' ].widget.attrs[ 'placeholder' ]="Enter name"

您可以将其传递给 modelformset_factory

AdvisorFormSet=modelformset_factory( Advisor, form=AdvisorForm )
于 2013-04-30T04:26:00.903 回答