11

I am trying to enable Django Reversion and Django-Import-Export for the same model... Not sure if my logic is correct or if I should be using multiple admin.py files etc

I have tried the following but this only allows Django Reversion to work, If I switch them around Import Export Works. It would be awesome if I could have both enabled at the same time.

class MyModelAdmin(reversion.VersionAdmin, ImportExportModelAdmin):
    pass

I have had a look at readthedocs for both Projects but am still lost.

http://django-reversion.readthedocs.org/en/latest/admin.html#admin https://django-import-export.readthedocs.org/en/latest/getting_started.html

Cheers xc0m

xc0m
  • 121
  • 5

4 Answers4

4

I ran into the same problem I fixed it by extending the Django admin change_list template and adding the links through that example below.

***change_list.html****    
{% extends "admin/change_list.html" %}

{% load i18n %} 

{% block object-tools %}
  {% if has_add_permission %}
    <ul class="object-tools ">
      {% block object-tools-items %}
        {% if not is_popup %}
          <li><a href="import/" class="import_link">{% trans "Import" %}</a></li>
          <li><a href="export/{{ cl.get_query_string }}" class="export_link">{% trans "Export" %}</a></li>
          <li><a href="{{recoverlist_url}}" class="recoverlink">{% blocktrans with cl.opts.verbose_name_plural|escape as name %}Recover deleted {{name}}{% endblocktrans %}</a></li>
        {% endif %}
       <li><a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}</a></li>
     {% endblock %}
   </ul>
 {% endif %}
{% endblock %}


***admin.py***
class MyAdmin(ImportExportMixin, MyModelAdmin):
    change_list_template = "change_list.html"
    resource_class = MyResource
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Aidan Doherty
  • 962
  • 2
  • 11
  • 29
  • 1
    I just wanted to add that this method works especially well if using **Django Grappelli**, you can just change the `
      ` to `
        ` and it keeps everything pretty! Hope this helps someone in the future!
    – Kinetic Stack May 27 '15 at 18:13
  • I created a new subclass that adds a similar `change_list_template = "change_list.html"` so this way, I didn't bloat all my models. – GabLeRoux Jul 25 '16 at 04:00
4

Final result:

import export recover deleted links

Inspired from @Aidan Doherty's answer, I created a new class that is a combination of VersionAdmin, ImportMixin, ExportMixin subclasses which extends a custom change_list_template.

Here's how it looks:

ImportExportVersionModelAdmin.py:

from import_export.admin import ImportMixin, ExportMixin
from reversion.admin import VersionAdmin


class ImportExportVersionModelAdmin(ImportMixin, ExportMixin, VersionAdmin):
    """
    Import, export and Version admin.
    Fixes missing link in change_list admin view :)
    """
    #: template for change_list view
    change_list_template = 'change_list_import_export_version.html'

templates/change_list_import_export_version.html:

{% extends "admin/import_export/change_list.html" %}
{% load i18n admin_urls %}

{% block object-tools-items %}
    <li><a href="import/" class="import_link">{% trans "Import" %}</a></li>
    <li><a href="export/{{ cl.get_query_string }}" class="export_link">{% trans "Export" %}</a></li>
    {% if not is_popup and has_add_permission and has_change_permission %}
        <li><a href="{% url opts|admin_urlname:'recoverlist' %}" class="recoverlink">{% blocktrans with cl.opts.verbose_name_plural|escape as name %}Recover deleted {{name}}{% endblocktrans %}</a></li>
    {% endif %}
    {{ block.super }}
{% endblock %}

This way I can use it like this:

class SiteAdmin(ImportExportVersionModelAdmin):
    pass


admin.site.register(Site, SiteAdmin)

It's directly inspired from import_export's admin.py ImportExportMixin and its change_list_import_export.html template, combined with reversion's template

Note: You can apply this solution to multiple subclasses :)

Community
  • 1
  • 1
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
1

I haven't done this with Reversion, but I think you'll want to use the ImportExportMixin:

from import_export.admin import ImportExportMixin

class MyModelAdmin(ImportExportMixin, reversion.VersionAdmin):
    pass

That's how I'm using it with another subclass, and it's working great.

Wendy
  • 111
  • 1
  • 8
  • Both `ImportExportMixin` or `ImportExportModelAdmin` are correct. There's only a small problem: Using this way, it fails to generate the urls in the `change_list` view in admin. It will only generate the links from the **first subclass**. (using `ImportExport` first will provide `import` and `export` links, but will hide `Recover deleted 'model'`. Good news is it's only a template problem. Append `/recover` to `change_list` url and you'll get to the right view. Edit model view will display `Show history` link. – GabLeRoux Jul 25 '16 at 03:48
0

not sure if this is still relevant but here is a quick solution:

{% extends "admin/change_list.html" %}
{% load i18n admin_urls %}

{% block object-tools-items %}
    {% if not is_popup and has_add_permission and has_change_permission %}
        <li><a href="{% url opts|admin_urlname:'recoverlist' %}" class="recoverlink">{% blocktrans with cl.opts.verbose_name_plural|escape as name %}Recover deleted {{name}}{% endblocktrans %}</a></li>
    {% endif %}
    {{ block.super }}
{% endblock %}

class CampaignAdmin(ImportExportModelAdmin, VersionAdmin):
  pass