You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is no way for a ModelView to stay on the create page or render a custom response after a successful create. The after_model_change hook returns None and cannot influence the HTTP response.
The only current workaround is to raise an exception from insert_model, which abuses exception-based control flow and causes the exception to be logged via logger.exception(e) in the create handler's except block, which makes no sense at all.
Use Case
One-time secret display after creating a resource (similar to how GitHub, and other platforms handle token creation). This is a common pattern for any admin who manages secrets, tokens, or credentials:
Admin creates an API key
The generated secret is shown once on the create page with a copy button
After navigating away, the secret is never shown again
Related Issues
#971 is somehow related. However, flash messages are designed for transient notifications shown on the next page after a redirect. A one-time secret needs to:
Stay on the current page (no redirect)
Be displayed prominently and persistently (not auto-dismissing)
Include a copy-to-clipboard button
Only disappear when the user explicitly navigates away
Describe the solution you would like.
Make the return value of after_model_change meaningful. Currently, it always returns None. Instead, allow it to return:
Checklist
Is your feature related to a problem? Please describe.
Problem
When overriding
insert_modelin aModelView, the create handler inapplication.pyalways issues a302redirect after a successful insert:There is no way for a
ModelViewto stay on the create page or render a custom response after a successful create. Theafter_model_changehook returnsNoneand cannot influence the HTTP response.The only current workaround is to raise an exception from
insert_model, which abuses exception-based control flow and causes the exception to be logged vialogger.exception(e)in the create handler's except block, which makes no sense at all.Use Case
One-time secret display after creating a resource (similar to how GitHub, and other platforms handle token creation). This is a common pattern for any admin who manages secrets, tokens, or credentials:
Related Issues
#971 is somehow related. However, flash messages are designed for transient notifications shown on the next page after a redirect. A one-time secret needs to:
Describe the solution you would like.
Make the return value of
after_model_changemeaningful. Currently, it always returnsNone. Instead, allow it to return:None- default redirect (fully backward-compatible)dict- re-render the create/edit template with extra context merged inResponse- return a custom Starlette Response directlyDescribe alternatives you considered
insert_modelto prevent the redirect - works but abuses control flow and pollutes logs vialogger.exception(e)Additional context
No response
I will happily contribute to this and give it a try.