How to call method/function in customised django admin template (change_from.html)

I am trying to customise a template in the admin panel for a specific model. I managed to override the template and can now change some html elements in the admin template.

What I want to do is to render a summary table within the change template, ideally calling {{summuary_table}}.somewhere in change_form template

I am struggling is to call a function/method in the new template.

I followed a few examples and settled on using the code below work. (DJANGO ADMIN Add_view, save_model, and change_view override example django admin · GitHub) I have been trying to stay away from CBV as much as possible as it seems a bit hard for my programming level but it looks like i don’t have a choice here.

I tried to make a basic instance work before starting something a bit more complicated, but even this is not working. Something else I am not clear about is that in this example (see admin.py) all responses I found on internet are referring to object_id instead of the mymodel_id.

Would be grateful for some pointers.

templates/admin/appname/model/change_form.html

{% extends "admin/change_form.html" %}
{% load i18n admin_urls %}
{% block object-tools-items %}
    <li>
        <a href="{% url opts|admin_urlname:'history' original.pk|admin_urlquote %}" class="historylink">{% translate "History" %}</a>
    </li>
    <li>
     
        {{summuary_table}}
    </li>
    {% if has_absolute_url %}
        <li>
            <a href="{% url 'admin:view_on_site' content_type_id original.pk %}" class="viewsitelink">{% translate "View on site" %}</a>
        </li>
    {% endif %}
{% endblock %}

models.py

class MyModel(models.Model):
    name = models.ForeignKey(OtherModel1,null=True, blank=True, on_delete=models.SET_NULL)
    user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
    status = models.BooleanField(default=False)

admin.py

class MyModelAdmin(admin.ModelAdmin):
     def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        summuary_table = MyModel.objects.filter(id=object_id)
        extra_context["form"] = self.get_form(instance=summuary_table, request=request)
        return super(MyModelAdmin, self).change_view(request, object_id, form_url=form_url, extra_context=extra_context)

Then you need to create a template (either complete or a block) that will render the data into a table format.

This isn’t a “call”, it’s a reference to an entry in the context.

What function or method are you trying to call?
Side note: Generally speaking, you don’t want to do work in the template. You want the work to be performed in the view, added to the context, and then rendered in the template.

That’s not going to work if you’re going to try and extend functionality in the admin. You’ll want to become very familiar with Python classes, Django CBVs, and the Django Admin classes.

It might be helpful if you provided more detail about exactly what you’re trying to do here. Talking about generalities tends to come up short when it comes to specific situations like this.

Thanks, that’s what I did (and left the code in my original post).
I am not even trying to render the table yet, just any data from the function would be a good start as it would be an indicator that the function is working.

This is detailed in the original post. I am looking to modify change_form. I am not doing it in the template, but in the admin.py which is apparently where this sort of logic should be happening.

> class MyModelAdmin(admin.ModelAdmin):
>      def change_view(self, request, object_id, form_url='', extra_context=None):
>         extra_context = extra_context or {}
>         summuary_table = MyModel.objects.filter(id=object_id)
>         extra_context["form"] = self.get_form(instance=summuary_table, request=request)
>         return super(MyModelAdmin, self).change_view(request, object_id, form_url=form_url, extra_context=extra_context)

I felt I was as detailed as I could in my previous post, which why I left some time to reply so I could find a better way to explain my situation.

I am looking to customise the change_form.html template in the admin panel by adding some data from a function contained in the admin.py. (recopied the code below)

class MyModelAdmin(admin.ModelAdmin):
     def change_view(self, request, object_id, form_url='', extra_context=None):
        extra_context = extra_context or {}
        summuary_table = MyModel.objects.filter(id=object_id)
        extra_context["form"] = self.get_form(instance=summuary_table, request=request)
        return super(MyModelAdmin, self).change_view(request, object_id, form_url=form_url, extra_context=extra_context)

Hopefully this is clearer.