soynatan / django-easy-audit

Yet another Django audit log app, hopefully the simplest one.
GNU General Public License v3.0
735 stars 182 forks source link

Cannot use Admin inlines for `CRUDEvent` #261

Open samamorgan opened 1 year ago

samamorgan commented 1 year ago

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 commented 9 months ago

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