2013-04-30 112 views
1

我正在使用modelformset_factory。使用modelformset_factory和佔位符

如何在生成的每個表單字段中添加佔位符文本?

例如,我有

AdvisorsFormSet = modelformset_factory( S_Advisors,MAX_NUM = 4,額外= 4,排除=( '啓動', 'ID'))

然後

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

如何在模板中呈現formset表單時生成的每個表單字段中添加佔位符文本?

的感謝!

+0

你的意思了場初始值? – Paulo 2013-04-30 02:17:14

回答

0

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)