fix(peewee): return None from ajax get_one() when no record matches - #2948
Open
DSeaStar wants to merge 1 commit into
Open
fix(peewee): return None from ajax get_one() when no record matches#2948DSeaStar wants to merge 1 commit into
DSeaStar wants to merge 1 commit into
Conversation
QueryAjaxModelLoader.get_one() raised DoesNotExist for a stale or absent pk, while AjaxSelectField expects None so the form can render with no selection. Match the sqla/mongoengine loaders and the view get_one() fix from pallets-eco#2939.
ElLorans
reviewed
Aug 21, 2026
| # can render with no selection rather than raising peewee's DoesNotExist | ||
| assert loader.get_one(999999) is None | ||
|
|
||
| with app.test_request_context("/admin/view/"): |
Collaborator
There was a problem hiding this comment.
Why do we need this part of the test?
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
QueryAjaxModelLoader.get_one()in the peewee backend calledself.model.get(...), which raisesDoesNotExistwhen no row matches.AjaxSelectField._get_dataalready guards withif model is not None, so a form that still holds a deleted (or otherwise unmatched) pk crashed instead of rendering with no selection.The sqla and mongoengine loaders already return
Noneon a miss, and the same peewee pattern was fixed forModelView.get_one()in #2939. This applies that change to the ajax loader left out of that PR.Closes #2940.
Test plan
test_ajax_fknow assertsloader.get_one(999999) is NoneNonepytest flask_admin/tests/peeweemodel/(13 passed)