dj-models: PrettyJSONMixin for admin JSONField display#10
Open
ashbrener wants to merge 1 commit into
Open
Conversation
Codify the PrettyJSONWidget + PrettyJSONMixin pattern that production codebases on this stack independently build in project/admin_utils.py. Django's default AdminTextareaWidget renders JSONField as a flat blob. The mixin wires formfield_for_dbfield to use a widget that json.dumps with indent=2 and sort_keys=True. Opt-in via mixin, not mandatory base class — only apply it where the model actually has a JSONField. Placed before admin.ModelAdmin in the MRO so the override takes effect.
daaf9d5 to
6e5d5e1
Compare
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.
Summary
Adds a small, self-contained utility —
PrettyJSONWidget+PrettyJSONMixin— that production codebases on this stack typically build independently. Django's defaultAdminTextareaWidgetrendersJSONFieldas a flat unindented blob; the widget pretty-prints withindent=2, sort_keys=True.Motivation
If you have a
JSONFieldanywhere in your models, the admin change view is hard to read without this. It's a ~15-line utility that gets reinvented in every project — worth codifying indj-modelsso the next project doesn't do it again.What changes
skills/dj-models/SKILL.md— new subsection under "Admin Rules":PrettyJSONWidget(AdminTextareaWidget)— pretty-prints JSONFieldPrettyJSONMixin— addsformfield_for_dbfieldoverride on any admin that opts inTradeoffs
BaseAdminModelso the admin is opt-in. A base class would tie every admin to the widget even when the model has noJSONField.ArrayField/HStoreFieldusers can extend the mixin.admin.ModelAdminso itsformfield_for_dbfieldfires first.