koendewit / django-client-side-image-cropping

Widget for the Django ImageField that provides an interface for cropping the image client-side to a specific size.
BSD 3-Clause "New" or "Revised" License
17 stars 11 forks source link

Django 3.2: Admin Mixin - TypeError: Cannot create a consistent method resolution #8

Open grahamhoyes opened 2 years ago

grahamhoyes commented 2 years ago

First off, this package is great and does just what I need it to!

I tried setting up the DcsicAdminMixin mixin on the admin site as the readme recommends, with an extra formfield override (not actually relevant to this example):

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin, DcsicAdminMixin):
   ...
   formfield_overrides = {
        models.ImageField: {
            "widget": ClientsideCroppingWidget(
                width=600, height=600, preview_width=150, preview_height=150
            )
        }
    }

However, this crashes the development server with the error:

TypeError: Cannot create a consistent method resolution
order (MRO) for bases ModelAdmin, DcsicAdminMixin

I'm running Django 3.2.7 and Python 3.8.

Is there a historical reason why DcsicAdminMixin itself subclasses admin.ModelAdmin? If not, the fix should just be to not have the mixin subclass anything (or subclass object), and change the order that the mixin is used in the docs:

class DcsicAdminMixin:
    class Media:
        css = ...
        js = ...

class MyAdmin(DcsicAdminMixin, admin.ModelAdmin):
    ...

If there's not a reason this is required for backwards compatability, I'd by happy to submit a PR with the changes. In the meantime, I'm just defining the Media class manually on my admin views.

grahamhoyes commented 2 years ago

It looks like just flipping the order of inheritance with the existing mixin satasfies MRO and works fine:

class MyAdmin(admin.ModelAdmin, DcsicAdminMixin):
    ...

That being said, removing the ancestor class from DcsicAdminMixin would still make sense and make it truly a mixin.