Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're having trouble having django accept the value you selected, because it doesn't think it should have been there in the first place, have the queryset refer to every possible value, but then override the "choices" attribute with an empty set. The latter part will avoid django construct a huge option list that is just going to be overridden by the dynamic javascript. </p> <p>I know this isn't drop-in-able for the admin screens, but I found this page while looking to do a similar thing for non-admin, so I thought I'd answer it here, too. Here's what I'm using:</p> <p>in views.py:</p> <pre><code>from django.shortcuts import render_to_response from django import forms from django.template import RequestContext from django.http import HttpResponseRedirect, HttpResponse from django.core.urlresolvers import reverse from django.core import serializers # other imports class AddRouteForm ( forms.Form ): router = forms.ModelChoiceField ( queryset=Router.objects.all() ) peer = forms.ModelChoiceField ( queryset=Peer.objects.all() ) peer.choices = [] # other stuff not relevant def get_peers ( request ): router_id = request.GET['router_id'] router = Router.objects.get(id=router_id) data = serializers.serialize ( "json", router.peer_set.filter(transit_capable=True) ) return HttpResponse ( data, content_type="text/json" ) def add_route ( request ): if request.method == "POST": form = AddRouteForm ( request.POST ) if form.is_valid (): # TODO something here return HttpResponseRedirect ( reverse ( "staticroute.views.index" ) ) else: form = AddRouteForm () return render_to_response ( "staticroute/add_route.html", locals(), RequestContext(request) ) </code></pre> <p>In the html file (jQuery needs to be loaded too):</p> <pre><code>&lt;form method="POST"&gt;{% csrf_token %} {{ form.as_p }} &lt;input type="submit" value="Add Route" /&gt; &lt;/form&gt; &lt;script&gt; $("#id_router").change ( function () { var router_id = $('#id_router').val(); var items = []; items.push ( '&lt;option value="" selected="selected"&gt;---------&lt;/option&gt;' ); $("#id_peer").html ( items.join('') ); if ( router_id != "" ) { $.getJSON ( '{% url staticroute.views.get_peers %}', {router_id:router_id}, function(data) { $.each ( data, function ( index, val ) { items.push ( '&lt;option value="' + val.pk + '"&gt;' + val.fields.description + ' (' + val.fields.address + ')&lt;/option&gt;'); } ); $("#id_peer").html ( items.join('') ); } ); } } ).change(); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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