feat: pre-fill create form with SQLAlchemy column defaults (#690)#771
Open
SHYXIN wants to merge 2 commits into
Open
feat: pre-fill create form with SQLAlchemy column defaults (#690)#771SHYXIN wants to merge 2 commits into
SHYXIN wants to merge 2 commits into
Conversation
When rendering the create form, extract default values from the SQLAlchemy model's column definitions and pass them as the initial `obj` to the template. This pre-fills form fields with their database-defined defaults. Adds BaseAdmin._get_default_values() which reads column.default.arg from the model's __table__. For non-SQLAlchemy models, returns an empty dict (no change in behavior).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #771 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 86 86
Lines 6848 6860 +12
=========================================
+ Hits 6848 6860 +12 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
When rendering the create form, extract default values from the SQLAlchemy model's column definitions and pass them as the initial
objto the template. This pre-fills form fields with their database-defined defaults.Changes
starlette_admin/base.py— Added_get_default_values()method that readscolumn.default.argfrom the model's__table___render_create()— On GET requests, calls_get_default_values()and passes the result asobjto the template contextHow it works
For SQLAlchemy models, the method iterates over
model.__table__.columnsand extractscol.default.argfor columns that have a Python-side default. For non-SQLAlchemy models, returns an empty dict (no change in behavior).Example: a column defined as
status = Column(String(20), default='active')will pre-fill the form field with'active'.Testing
Additional
Includes chore commit to fix pyproject.toml pytest config conflict.