Skip to content

fix(peewee): return None from ajax get_one() when no record matches - #2948

Open
DSeaStar wants to merge 1 commit into
pallets-eco:masterfrom
DSeaStar:fix/peewee-ajax-get-one-missing
Open

fix(peewee): return None from ajax get_one() when no record matches#2948
DSeaStar wants to merge 1 commit into
pallets-eco:masterfrom
DSeaStar:fix/peewee-ajax-get-one-missing

Conversation

@DSeaStar

Copy link
Copy Markdown

Summary

QueryAjaxModelLoader.get_one() in the peewee backend called self.model.get(...), which raises DoesNotExist when no row matches. AjaxSelectField._get_data already guards with if 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 None on a miss, and the same peewee pattern was fixed for ModelView.get_one() in #2939. This applies that change to the ajax loader left out of that PR.

def get_one(self, pk):
    try:
        return self.model.get(**{self.pk: pk})
    except DoesNotExist:
        return None

Closes #2940.

Test plan

  • test_ajax_fk now asserts loader.get_one(999999) is None
  • Creating a form and feeding a missing pk no longer raises; the field data stays None
  • Full peewee suite: pytest flask_admin/tests/peeweemodel/ (13 passed)

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.
# 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/"):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we need this part of the test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

fix(peewee): QueryAjaxModelLoader.get_one() raises DoesNotExist for a stale/absent pk

2 participants