Search code examples
jqueryajaxdjangoadmin

Django admin change_form with ajax query


I'm working with an ModelAdmin from my model. I have a field that I need to check if it's right.

It's a string and I'm sending it to the server, and this returns me a graph in which I can check the string if it's ok.

The big question is about how to use ajax with the admin view, and the view returns the object to me. It's a little different from the classic way with ajax+views, because i have the admin mode class inside admin.py. I redefined the change_form template and added the js and ajax calls. But it returns me nothing at the moment.

Thanks for your advice!


Solution

  • Place your change_form.html in the below hierachy:

    /myproject/templates/admin/myapp/mymodel/change_form.html
    

    And inherit the base change_form.html from admin. Eg:

    {% extends "admin/change_form.html" %}
    {% block after_field_sets %}{{ block.super }}
    Print my model here {{ original }}
    Print foreignkey related records of my model:
    {% for item in original.items_set.all %} {{ item }} {% endfor %}
    {% endblock %}