是否可以在can_delete=True
时更改modelformset_factory中删除字段的标签?
forms.py:
class LayerMapOptionsForm(forms.ModelForm):
layerLink = forms.CharField(label='layer', required=False)
styleLink = forms.CharField(label='style', required=False)
labelLink = forms.CharField(label='label', required=False)
def __init__(self, *args, **kwargs):
super(LayerMapOptionsForm, self).__init__(*args, **kwargs)
self.fields['layerLink'].widget = LayerLinkWidget(self.instance.layer)
self.fields['styleLink'].widget = LayerStyleLinkWidget(self.instance)
self.fields['labelLink'].widget = LayerLabelLinkWidget(self.instance)
class Meta:
model = LayerMapOptions
fields = ['layerLink', 'styleLink', 'labelLink', 'visible']
widgets= {
'id' : forms.HiddenInput(attrs={'class': 'layer_id'})
}
views.py:
layers_ordered = LayerMapOptions.objects.filter(basqui_map=map_selected) \
.order_by('position')
layerForm = modelformset_factory(LayerMapOptions, form=LayerMapOptionsForm,
extra=0, can_delete=True)
formset = layerForm(queryset=layers_ordered)
答案 0 :(得分:2)
是的,有可能。覆盖基本formset的add_fields()
方法:
from django.forms.models import BaseModelFormSet
from django.forms.formsets import DELETION_FIELD_NAME
class LayerMapOptionsFormSet(BaseModelFormSet):
def add_fields(self, form, index):
super(LayerMapOptionsFormSet, self).add_fields(form, index)
form.fields[DELETION_FIELD_NAME].label = 'To be or not to be?'
layerForm = modelformset_factory(LayerMapOptions, form=LayerMapOptionsForm,
formset=LayerMapOptionsFormSet,
extra=0, can_delete=True)