is_valid()
フォームセットは検証に合格しますが、テンプレートで{{ formset.errors }}
返されます。[{}, {}, {}, {}]
(modelformset_factory フォームセットのフォームごとに 1 つの {}。
とにかくフォームが正常に保存されることを理解していません...
def setMapOptions(request, map_name):
map_selected = EzMap.objects.get(map_name=map_name, created_by=request.user)
layers_ordered = LayerMapOptions.objects.filter(ezmap=map_selected).order_by('position')
layerForm = modelformset_factory(LayerMapOptions, form=LayerMapOptionsForm, extra=0)
if request.POST:
pk_list = [int(x) for x in request.POST.get("layersOrder").split(',')]
formset = layerForm(request.POST, queryset=layers_ordered)
if formset.is_valid():
for form in formset:
instance = form.instance
instance.position = pk_list.index(instance.pk)
instance.save()
else:
formset = ezMapOptionFormSet(user=request.user, instance=map_selected)
formset2 = layerForm(queryset=layers_ordered)
tmsURL = "http://" + request.get_host() + "/ezmapping/tms/"
return render_to_response("ezmapping/manage_map_Options.html", {'formset': formset, 'map': map_selected, 'layersOrder': layers_ordered, 'tmsURL' : tmsURL, }, context_instance=RequestContext(request))
形:
class ModelLinkWidget(forms.Widget):
def __init__(self, obj, attrs=None):
self.object = obj
super(ModelLinkWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
if self.object.pk:
return mark_safe(
u'<a href="/ezmapping/layer/edit/%s">%s</a>' %\
(
self.object.pk, self.object
)
)
else:
return mark_safe(u'')
class LayerMapOptionsForm(ModelForm):
link = forms.CharField(label='link', required=False)
def __init__(self, *args, **kwargs):
super(LayerMapOptionsForm, self).__init__(*args, **kwargs)
self.fields['link'].widget = ModelLinkWidget(self.instance.layer)
class Meta:
model = LayerMapOptions
fields = ['link', 'visible']