简体   繁体   中英

using modelformset_factory and placeholder

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!

modelformset_factory takes a form keyword argument.

So if you create a 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"

you can pass it to modelformset_factory

AdvisorFormSet=modelformset_factory( Advisor, form=AdvisorForm )

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