First of all, I recommend that you read chapter 17 of Django Book (if
you haven't already)
http://www.djangobook.com/en/1.0/chapter17/
Moving on, you have to create a view, as we are working on Django's
admin, then i recommend that you create one like project/app/
admin_views.py with something similar to this code:
from indica.geral.models import *
from django.http import HttpResponse
from django.template import RequestContext
from django.utils import simplejson
from django.contrib.admin.views.decorators import
staff_member_required
def country_getStates(request):
id = request.GET.get('id','')
result = [u.__dict__ for u in State.objects.filter(country=id)]
return HttpResponse(simplejson.dumps(result), mimetype='application/
javascript')
country_getStates = staff_member_required(country_getStates)
Then, you have to customize the change_form.html template with the JS
call, something like this (as you can see, i am using JQuery):
{% extends "admin/change_form.html" %}
{% block extrahead %}
<script src="
http://SERVERNAME/js/jquery-1.2.3.min.js" type="text/
javascript"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
$("select#id_country").change(function(){
$.getJSON("/admin/bd1/country/getStates/",{id: $(this).val()},
function(j){
var options = '<option value="">---------</option>';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].id + '">' + j[i].state +
'</option>';
}
$("select#id_state").html(options);
})
})
})
</script>
{% endblock %}
Create a JS file and put this second script tag in it, that way will
be easier to maintain your template.
At urls.py you need to add a pattern to reach the view:
(r'^admin/bd1/country/getStates/$',
'project.app.admin_views.country_getStates'),
This is the classic example of a country -> state interaction, the
same for your category -> subcategory need.
[]s,
Diego Ucha