mirror of
https://github.com/nikdoof/vapemap.git
synced 2025-12-24 15:19:21 +00:00
Added very basic Tastypie API, and dynamic forms for Country/County.
This commit is contained in:
18
app/stores/api.py
Normal file
18
app/stores/api.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from django.forms.models import model_to_dict
|
||||
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
|
||||
from stores.models import County, Country
|
||||
|
||||
|
||||
class CountryResource(ModelResource):
|
||||
|
||||
def dehydrate(self, bundle):
|
||||
counties = County.objects.filter(country=bundle.data['id'])
|
||||
bundle.data['counties'] = [model_to_dict(c) for c in counties]
|
||||
return bundle
|
||||
|
||||
class Meta:
|
||||
queryset = Country.objects.all()
|
||||
resource_name = 'country'
|
||||
filtering = {
|
||||
'id': ('exact',)
|
||||
}
|
||||
@@ -9,6 +9,25 @@
|
||||
{% block scripts %}
|
||||
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
|
||||
<script type="text/javascript" src="{% static "js/geocoder.js" %}"></script>
|
||||
<script type="text/javascript">
|
||||
$(document).ready(function() {
|
||||
$('select#id_0-county').html('').prop('disabled', 'disabled');
|
||||
$('select#id_0-country').change(function(){
|
||||
var country_id = $("select#id_0-country option:selected").val();
|
||||
var url = '{% url 'api_dispatch_list' resource_name='country' api_name='1.0' %}' + country_id + "/";
|
||||
$('select#id_0-county').html('').prop('disabled', 'disabled');
|
||||
$.getJSON(url, function(data) {
|
||||
if (data.counties.length > 0) {
|
||||
$(data.counties).each(function () {
|
||||
$('select#id_0-county').append($('<option/>').val(this.id).text(this.name));
|
||||
});
|
||||
$('select#id_0-county').change();
|
||||
$('select#id_0-county').prop('disabled', false);
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
Reference in New Issue
Block a user