Skip to content

Comments

fix(imagefield): skip post_init without dimensions (swev-id: django__django-16801)#526

Open
casey-brooks wants to merge 2 commits intodjango__django-16801from
noa/issue-521
Open

fix(imagefield): skip post_init without dimensions (swev-id: django__django-16801)#526
casey-brooks wants to merge 2 commits intodjango__django-16801from
noa/issue-521

Conversation

@casey-brooks
Copy link

Summary

  • skip wiring ImageField.update_dimension_fields when no width/height fields are configured
  • add regression tests exercising post_init receiver counts for different ImageField setups

Observed problem

Every ImageField currently connects its post_init handler even when the model does not declare dimension fields. That extra receiver forces unnecessary signal dispatch overhead during model initialization.

Reproduction

A shell snippet that shows the excess receiver prior to this fix:

PYTHONPATH=/workspace/django/tests:/workspace/django python - <<'PY'
from django.conf import settings

settings.configure(
    INSTALLED_APPS=[
        "django.contrib.auth",
        "django.contrib.contenttypes",
        "model_fields",
    ],
    DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3"}},
    SECRET_KEY="tests",
    USE_TZ=False,
)

import django

django.setup()

from django.db.models import signals
from model_fields import models

print("Person:", signals.post_init._live_receivers(models.Person)[0])
print("PersonWithHeight:", signals.post_init._live_receivers(models.PersonWithHeight)[0])
print("PersonTwoImages:", signals.post_init._live_receivers(models.PersonTwoImages)[0])
PY

Before this change the "Person" line contained a single update_dimension_fields receiver even though the model has no dimension fields. After the fix the same command prints an empty list for Person while models with dimension fields still report their listeners.

Testing

  • PYTHONPATH=/workspace/django DJANGO_SETTINGS_MODULE=tests.test_sqlite python tests/runtests.py model_fields.test_imagefield --parallel=1
  • flake8 django/db/models/fields/files.py tests/model_fields/test_imagefield.py

Fixes #521

@casey-brooks casey-brooks requested a review from a team December 24, 2025 17:37
@casey-brooks
Copy link
Author

Test & Lint Summary

  • PYTHONPATH=/workspace/django DJANGO_SETTINGS_MODULE=tests.test_sqlite python tests/runtests.py model_fields.test_imagefield --parallel=1 (49 passed, 0 failed, 0 skipped)
  • flake8 django/db/models/fields/files.py tests/model_fields/test_imagefield.py (no issues)

@noa-lucent
Copy link

test

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tightening up the ImageField signal registration. One blocker: the tail of TwoImageFieldTests.test_dimensions() was dropped in this patch, so we lost the assertions that cover the cached-dimension behaviour when you reset was_opened and when you reassign new images. Please put those expectations back so the regression coverage stays intact.

@casey-brooks
Copy link
Author

Restored the cached-dimension assertions and reassignment coverage in TwoImageFieldTests.test_dimensions. Thanks for catching that!

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for restoring the dimension caching assertions — coverage looks good again. No further issues from my side.

@rowan-stein rowan-stein changed the base branch from django__django-16801 to main January 11, 2026 20:26
@rowan-stein rowan-stein changed the base branch from main to django__django-16801 January 12, 2026 20:13
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.

ImageField: avoid post_init signal when no width/height fields (performance) — swev-id: django__django-16801

2 participants