django-easy-audit icon indicating copy to clipboard operation
django-easy-audit copied to clipboard

Cannot use Admin inlines for `CRUDEvent`

Open samamorgan opened this issue 1 year ago • 1 comments

The way CRUDEvent is currently designed prevents generic inlines from functioning. Currently, object_id is a CharField. I'd propose that this be changed to a GenericForeignKey so built-in Django functionality works as expected.

Expected to work:

class CrudEventInline(GenericTabularInline):
    model = CRUDEvent

Error:

⋊> python manage.py check
SystemCheckError: System check identified some issues:

ERRORS:
<class 'app.model.admin.CrudEventInline'>: (admin.E301) 'easyaudit.CRUDEvent' has no GenericForeignKey.

samamorgan avatar Oct 12 '23 21:10 samamorgan

I found a workaround for this, but it's stinky. I'm still of the opinion that object_id should be a GenericForeignKey.

from django.contrib.contenttypes.admin import (
    GenericInlineModelAdminChecks,
    GenericTabularInline,
)
from easyaudit.models import CRUDEvent


class CRUDEventGenericCheck(GenericInlineModelAdminChecks):
    """Override check against `ct_fk_field`.

    easyaudit uses CharField instead of GenericForeignKey for `ct_fk_field`
    """

    def _check_relation(self, obj, parent_model):
        errors = super()._check_relation(obj, parent_model)

        for error in list(errors):
            if error.msg == "'easyaudit.CRUDEvent' has no GenericForeignKey.":
                errors.remove(error)

        return errors


class CRUDEventInline(GenericTabularInline):
    model = CRUDEvent

    checks_class = CRUDEventGenericCheck

samamorgan avatar Feb 01 '24 23:02 samamorgan