Reputation: 11
I saw the original post Populating values in django-admin based on a foreign key selection but did not understand the template extending example:
{% extends "admin/change_form.html" %}
{% block extrahead %}
$(document).ready(function() {
$('#id_template').change(function() {
$.ajax({
type: 'POST',
url: "{% url get_template_info %}",
data: {'template_id': $('#id_template').val()},
success: function(data, _status) {
$('#id_throttle').val(data.throttle);
$('#id_rate_limit').val(data.rate_limit);
$('#id_product').val(data.product);
$('#id_tier_group').val(data.tier_group);
},
dataType: "json"
});
});
});
{% endblock %}
I am trying to do something similar, but do not understand where the url get_template_info comes from.
I am assuming should be a method that belongs to TemplateAdmin(admin.ModelAdmin) but I am not sure about that. the only clue in django documentation was https://docs.djangoproject.com/en/dev/ref/contrib/admin/ which puts an example
class MyModelAdmin(admin.ModelAdmin):
# A template for a very customized change view:
change_form_template = 'admin/myapp/extras/openstreetmap_change_form.html'
def get_osm_info(self):
# ...
pass
def change_view(self, request, object_id, extra_context=None):
extra_context = extra_context or {}
extra_context['osm_data'] = self.get_osm_info()
return super(MyModelAdmin, self).change_view(request, object_id,
extra_context=extra_context)
But tend to be more cryptic!, I am new on Django so please give me some guidance on this thing.
Upvotes: 1
Views: 268
Reputation: 239260
The author of that answer wasn't very comprehensive, but there's a hint in the last line:
which hits a view that simply takes the id passed in, queries it up and returns it to the call. Works like a charm.
get_template_info
isn't a built in part of Django. It's the name of the view he created to handle the AJAX request. You'll have to create your own view to return the information you need.
Upvotes: 1