I have a Django view that I'm calling via an Ajax function in a template. My template has a 'Country' pulldown and a 'Region' (or State) pulldown. When the user clicks on a Country, I want to use an Ajax function to fetch all the regions (or states) in that country and populate a 'Region' dropdown with that data. However, when my view executes and tries to return the query set, I'm getting this error:
ValueError: too many values to unpack
Here is the template tag and Ajax function:
# demo.html
<select id="id_country" name="country" onchange="getState(this.value);">
<option value="" selected="selected">Please choose...</option>
<option value="US">United States</option>
<option value="GB">United Kingdom</option>
<option value="CA">Canada</option>
</select>
<script>
function getState(val) {
$.ajax({
type: "GET",
url: "/demo/get_region",
data:'country_id='+val,
success: function(data){
$("#id_region").html(data);
}
});
}
</script>
Here is the view:
# views.py
from location.models import CountryRegion
def get_region(request):
country_id = request.GET.get('country_id', None)
country_region = CountryRegion.objects.filter(country_cd=country_id)
# Error occurs here on the return
return country_region
The model (which I realize isn't normalized) looks like this:
class CountryRegion(models.Model):
country_cd = models.CharField(max_length=2)
country_name = models.CharField(max_length=50)
region_cd = models.CharField(max_length=3)
region_name = models.CharField(max_length=50)
A database record looks like this:
id country_cd country_name region_cd region_name
59 US United States NY New York
Why is this error occurring? If the country code is 'US', I would be returning 50 instances, one for each state. Is there some limit to the amount of data a Django view can return?