Add register_encoder hook for custom API value serialization (#136)#138
Merged
Conversation
Model field values are serialized to JSON-safe representations in BaseModelAdmin.serialize_obj_attributes. Add a small user-facing registry (fastadmin/api/encoders.py) so users can control how a given type is rendered in every admin API response — e.g. a custom datetime format, an Enum's label, or a domain value object. Custom encoders are matched via isinstance in registration order and take precedence over the built-in datetime/UUID/Decimal handling, which also provides the configurable datetime output requested in #135. - fastadmin/api/encoders.py: register_encoder / unregister_encoder / clear_encoders / apply_custom_encoders + CUSTOM_ENCODERS registry - base.py: apply custom encoders before the built-in Decimal formatting - expose register_encoder / unregister_encoder from the package root - docs + changelog + unit/integration tests Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Merged
vsdudakov
added a commit
that referenced
this pull request
Jul 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #136. Also delivers the configurable-format enhancement noted in #135.
What
Adds a small user-facing registry so you can control how a given type is serialized in every admin API response — a custom datetime format, an
Enum's label, a domain value object, etc.Why
Distilled from the fork analysis of this repo. The alex-averin fork added a
CUSTOM_ENCODERSdict wired into FastAPI responses (commit, "Add custom encoders for fastapi"). This PR generalizes that idea into a framework-agnostic hook.Approach
Rather than patch each of the three framework JSON encoders (FastAPI / Flask / Django), custom encoders are applied at the single framework-agnostic serialization point —
BaseModelAdmin.serialize_obj_attributes— so all frameworks and ORMs benefit uniformly, and the values reaching each framework's response layer are already JSON-safe.fastadmin/api/encoders.py—register_encoder/unregister_encoder/clear_encoders/apply_custom_encodersover aCUSTOM_ENCODERSregistry.fastadmin/models/base.py— apply custom encoders before the built-inDecimalformatting; a matching encoder takes precedence.fastadmin/__init__.py— exportregister_encoder/unregister_encoderfrom the package root.isinstancein registration order (register more specific types before base types).Relationship to #135
The concrete "datetime read error" that three forks patched is already fixed in the current codebase (all three frameworks serialize
datetime/date/timeto ISO 8601, andbase.pydeserializes the date/time pickers). The one remaining ask in #135 — configurable output format — is delivered here:register_encoder(datetime.datetime, ...).Tests
tests/api/test_encoders.py— registry unit tests (match order, override, unregister, clear).tests/models/test_base.py— integration: encoder applied during serialization + overrides the built-inDecimalformatting.ruff check,ruff format --check, andty checkall clean.🤖 Generated with Claude Code