Django - Modelform is_valid()=True but form.errors returns [{}, {}, {}, {}] -


the formset pass is_valid() verification {{ formset.errors }} returns [{}, {}, {}, {}] in template. (one {} each form in modelformset_factory formset.

i dont understand form saved anyway...

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) x in request.post.get("layersorder").split(',')]         formset = layerform(request.post, queryset=layers_ordered)         if formset.is_valid():             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)) 

form:

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'] 

formset.errors list entries correspond forms. when formset valid, there no errors on individual forms, therefore formset.errors list of empty dictionaries.

this means formset.errors evaluate true when formset valid, because non empty list evaluates true. gotcha len(formset.errors) returns number of forms in formset, not number of errors. in django 1.6 there new formset.total_error_count() method solve problem.

note there formset.non_form_errors() method, returns errors not related particular form.

see docs on formset validation more information.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -