Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom change_list_template in admin views using mixins #1483

1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ The following is a list of much appreciated contributors:
* frgmt
* vanschelven (Klaas van Schelven)
* HaPyTeX (Willem Van Onsem)
* nikhaldi (Nik Haldimann)
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog

- bugfix: `skip_row()` fix crash when model has m2m field and none is provided in upload (#1439)
- Fix deprecation in example application: Added support for transitional form renderer (#1451)
- Allow custom `change_list_template` in admin views using mixins (#1483)
- Fixed Makefile coverage: added `coverage combine`

3.0.0-beta.4 (2022-05-17)
Expand Down Expand Up @@ -60,6 +61,9 @@ This release makes some minor changes to the public API. If you have overridden
- ImportForm: improve compatibility with previous signature (#1434)
- Previous `ImportForm` implementation was based on Django's `forms.Form`, if you have any custom ImportForm you now need to inherit from `import_export.forms.ImportExportFormBase`.

- Allow custom `change_list_template` in admin views using mixins (#1483)
- If you are using admin mixins from this library in conjunction with code that overrides `change_list_template` (typically admin mixins from other libraries such as django-admin-sortable2 or reversion), object tools in the admin change list views may render differently now.

Deprecations
############

Expand Down
32 changes: 28 additions & 4 deletions import_export/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,34 @@


class ImportExportMixinBase:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# Store already set change_list_template to allow users to independently
# customize the change list object tools. This treats the cases where
# `self.change_list_template` is `None` (the default in `ModelAdmin`) or
# where `self.import_export_change_list_template` is `None` as falling
# back on the default templates.
if getattr(self, 'change_list_template', None):
pokken-magic marked this conversation as resolved.
Show resolved Hide resolved
self.base_change_list_template = self.change_list_template
else:
self.base_change_list_template = 'admin/change_list.html'

self.change_list_template = getattr(
self, 'import_export_change_list_template', None
)
if self.change_list_template is None:
self.change_list_template = self.base_change_list_template

def get_model_info(self):
app_label = self.model._meta.app_label
return (app_label, self.model._meta.model_name)

def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {}
extra_context['base_change_list_template'] = self.base_change_list_template
return super().changelist_view(request, extra_context)


class ImportMixin(BaseImportMixin, ImportExportMixinBase):
"""
Expand All @@ -47,7 +71,7 @@ class ImportMixin(BaseImportMixin, ImportExportMixinBase):
"""

#: template for change_list view
change_list_template = 'admin/import_export/change_list_import.html'
import_export_change_list_template = 'admin/import_export/change_list_import.html'
#: template for import view
import_template_name = 'admin/import_export/import.html'
#: available import formats
Expand Down Expand Up @@ -547,7 +571,7 @@ class ExportMixin(BaseExportMixin, ImportExportMixinBase):
https://docs.djangoproject.com/en/dev/ref/contrib/admin/
"""
#: template for change_list view
change_list_template = 'admin/import_export/change_list_export.html'
import_export_change_list_template = 'admin/import_export/change_list_export.html'
#: template for export view
export_template_name = 'admin/import_export/export.html'
#: export data encoding
Expand Down Expand Up @@ -709,7 +733,7 @@ class ImportExportMixin(ImportMixin, ExportMixin):
Import and export mixin.
"""
#: template for change_list view
change_list_template = 'admin/import_export/change_list_import_export.html'
import_export_change_list_template = 'admin/import_export/change_list_import_export.html'


class ImportExportModelAdmin(ImportExportMixin, admin.ModelAdmin):
Expand All @@ -724,7 +748,7 @@ class ExportActionMixin(ExportMixin):
"""

# Don't use custom change list template.
change_list_template = None
import_export_change_list_template = None

def __init__(self, *args, **kwargs):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "admin/change_list.html" %}
{% extends base_change_list_template %}

{# Original template renders object-tools only when has_add_permission is True. #}
{# This hack allows sub templates to add to object-tools #}
Expand Down
1 change: 1 addition & 0 deletions tests/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BookAdmin(ImportExportMixin, admin.ModelAdmin):
list_display = ('name', 'author', 'added')
list_filter = ['categories', 'author']
resource_classes = [BookResource, BookNameResource]
change_list_template = "core/admin/change_list.html"


class CategoryAdmin(ExportActionModelAdmin):
Expand Down
9 changes: 9 additions & 0 deletions tests/core/templates/core/admin/change_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "admin/change_list.html" %}
matthewhegarty marked this conversation as resolved.
Show resolved Hide resolved
{% comment %}
A template used for testing customizations to the change_list view (See #1483).
{% endcomment %}

{% block object-tools-items %}
<!-- <li>Custom change list item</li> -->
{{ block.super }}
{% endblock %}
3 changes: 3 additions & 0 deletions tests/core/tests/test_admin_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ def test_import_export_template(self):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response,
'admin/import_export/change_list_import_export.html')
self.assertTemplateUsed(response, 'admin/change_list.html')
self.assertTemplateUsed(response, 'core/admin/change_list.html')
self.assertContains(response, _('Import'))
self.assertContains(response, _('Export'))
self.assertContains(response, 'Custom change list item')
pokken-magic marked this conversation as resolved.
Show resolved Hide resolved

@override_settings(TEMPLATE_STRING_IF_INVALID='INVALID_VARIABLE')
def test_import(self):
Expand Down