Sudipto Sarker
Sudipto Sarker

Reputation: 155

How to use django-admin-sortable2 and django-import-export package together in admin panel?

I want to use django-admin-sortable2 django-import-export package together in admin panel. Here is the code:

class University(models.Model):
    name = models.CharField(max_length=50, help_text="University or Institution Name")
    short_name = models.CharField(blank=True, max_length=10, help_text="University or Institution 
 Short Name")
    order = models.PositiveIntegerField(
        default=0,
        blank=False,
        null=False,
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f'{self.id}-{self.name}' 

    class Meta:
        ordering = ['order'] 

admin.py:

from import_export import resources
from import_export.admin import ImportExportModelAdmin

from adminsortable2.admin import SortableAdminMixin
# Register your models here.

class UniversityResource(resources.ModelResource):
    class Meta:
        model = University
        exclude = ('created_at', 'updated_at',)


class UniveresityAdmin(ImportExportModelAdmin, SortableAdminMixin, admin.ModelAdmin):
    resource_classes = [UniversityResource]

admin.site.register(University, UniveresityAdmin)
  

But this is not working, If I use:

class UniveresityAdmin(ImportExportModelAdmin, SortableAdminMixin, admin.ModelAdmin):
    resource_classes = [UniversityResource]

Console:

failed to assign change_list_template attribute (see issue 1521)

This is the output:
enter image description here

If I use:

class UniveresityAdmin(ImportExportModelAdmin, SortableAdminMixin,):

This provides: enter image description here And if I use:

class UniveresityAdmin(SortableAdminMixin, ImportExportModelAdmin,):

This provides:

TypeError at /admin/core/university/
join() argument must be str, bytes, or os.PathLike object, not 'list'

Both works single handed if I remove either one of them. But I want to use both package together. Is there any workaround to solve this issue? Thanks in advance.

Upvotes: 0

Views: 424

Answers (2)

Andro Besalashvili
Andro Besalashvili

Reputation: 1

Try:

class SomeAdmin(SortableAdminMixin,ImportExportMixin , admin.ModelAdmin):

should work..

P.S Do not forget to import ImportExportMixin:

from import_export.admin import ImportExportModelAdmin, ImportExportMixin, 

Upvotes: 0

Matthew Hegarty
Matthew Hegarty

Reputation: 4306

The short answer is that import-export and admin-sortable2 don't work together. AFAIK there is no workaround.

The reason is that admin-sortable2 defines change_list_template as a property meaning it cannot be reset. In import-export, all we can do is detect this and log the error:

failed to assign change_list_template attribute (see issue 1521)

The link to this issue is here. There is an open issue on admin-sortable2.

Upvotes: 2

Related Questions