Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango Dajax validating model with many-to-many field
    text
    copied!<p>I am using django and dajax and I am trying to validate an item that has a many-to-many field. I am using the django forms to create the form for this model. When I submit the form with a submit button and have at least 1 to x number of items selected in the selectbox, the model gets validated. When I use dajax, the model will only validate if I have atleast 2 of the items in the select box are selected.</p> <p>Does anyone know why this would happen?</p> <p>Model</p> <pre><code>#Hints class Hint(models.Model): title = models.TextField(max_length=200) body = models.TextField() industryType = models.ManyToManyField(IndustryType) screen = models.ManyToManyField(Screen) #Creates form for a hint class HintForm(ModelForm): class Meta: model = Hint #widgets = {'industryType': CheckboxSelectMultiple, 'screen': CheckboxSelectMultiple } industryType = ModelMultipleChoiceField(queryset=IndustryType.objects.all(), widget=CheckboxSelectMultiple()) </code></pre> <p>Template:</p> <pre><code>&lt;form id="hintForm" action="." method="post"&gt; {% csrf_token %} &lt;table&gt; {{ form.as_table }} &lt;/table&gt; &lt;input type="submit" value="Submit"/&gt; &lt;/form&gt; &lt;input type="button" onclick="send_form();" value="Add Hint" /&gt; &lt;script&gt; function send_form(){ data = $('#hintForm').serializeObject(); // jQuery // If you are using jQuery, you need this form-&gt;object serializer // https://github.com/cowboy/jquery-misc/blob/master/jquery.ba-serializeobject.js alert(data['screen']) Dajaxice.THE.send_form(Dajax.process,{'hintform':data}); return(false) } &lt;/script&gt; </code></pre> <p>Code in the View</p> <pre><code>def addHint(request): if request.method == 'POST': # If the form has been submitted... form = HintForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data #savedHint = form.save() #messages.success(request, '{0} has been added.'.format(savedHint)) print "Success" else: form = HintForm() # An unbound form return render_to_response('manage/screens/form.html', { 'form': form, 'breadcrumName' : 'Add Screen' }, context_instance=RequestContext(request)) </code></pre> <p>AJAX.py</p> <pre><code>@dajaxice_register def send_form(request, hintform): dajax = Dajax() form = HintForm(hintform) print form if form.is_valid(): dajax.alert("This form is_valid") else: dajax.alert("Not Valid") return dajax.json() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload