pythondjangotemplatesviewdjango-grappelli

Overriding default view and template for a model in django / passing variables to grapelli template


I am using django admin and grappelli and wanted to override the default view for a certain model.

I overrode the grapelli change_form template:

```

{% extends "grappelli:admin/change_form.html" %}
{% block javascripts %}
    {{ block.super }}
    <script type="text/javascript">{{Object}}</script>
 {% endblock %}

```

I have 2 questions:

  1. How can I access the model that is passed by default by django? Neither Object nor opts seem to work.
  2. How do I override the default to view (what's the method I need to override) to add new variables into template context?

Thanks in advance.


Solution

  • you can override with change_view in admin.py and pass variables through extra_context. e.g:

    class GivenModel(models.Model):
        def change_view(self, request, object_id, form_url='', extra_context=None):
    
                    your_obj = GivenModel.objects.get(id=asset_id.group(1))
                    extra_context = {'title': your_obj.description}
                return super(GivenModel, self).change_view(request, object_id, form_url, extra_context)