Skip to content

fix: MiddlewareEvents breaking import without user model app installed - #259

Open
juliencloud wants to merge 1 commit into
AmbitionEng:mainfrom
loft-orbital:fix-middleware-events-without-user-model
Open

fix: MiddlewareEvents breaking import without user model app installed#259
juliencloud wants to merge 1 commit into
AmbitionEng:mainfrom
loft-orbital:fix-middleware-events-without-user-model

Conversation

@juliencloud

Copy link
Copy Markdown

Summary

MiddlewareEvents.user is always declared as a ForeignKey to settings.AUTH_USER_MODEL, which fails when the user model's app is not in INSTALLED_APPS. Django can fail to start even if you never use MiddlewareEvents, because the model is defined at import time.

This change checks whether the user model's app is installed and falls back to a TextField (same as url) when it is not.

Changes

  • Add _middleware_events_user_field() in pghistory/models.py
  • Conditionally define MiddlewareEvents.user as ForeignKey or TextField
  • Document the behavior in docs/aggregating_events.md
  • Add unit tests for both field paths

Test plan

  • make test — passed
  • make lint — ruff and docs passed
  • make full-test-suite — all tox pytest envs passed

Thanks for maintaining django-pghistory! Happy to adjust anything based on feedback.

MiddlewareEvents.user was always a ForeignKey to AUTH_USER_MODEL, which
fails when the user model's app is not in INSTALLED_APPS. Fall back to
a TextField proxy when the app is unavailable, matching how url is
handled.

Add _middleware_events_user_field() so both paths are testable, and
document the behavior in aggregating_events.md.
@juliencloud
juliencloud force-pushed the fix-middleware-events-without-user-model branch from a6c144d to b2d5cda Compare July 13, 2026 20:39
@juliencloud juliencloud changed the title Fix MiddlewareEvents breaking import without user model app installed fix: MiddlewareEvents breaking import without user model app installed Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant