Skip to content

feat: pre-fill create form with SQLAlchemy column defaults (#690)#771

Open
SHYXIN wants to merge 2 commits into
jowilf:mainfrom
SHYXIN:feat/prefill-form-defaults
Open

feat: pre-fill create form with SQLAlchemy column defaults (#690)#771
SHYXIN wants to merge 2 commits into
jowilf:mainfrom
SHYXIN:feat/prefill-form-defaults

Conversation

@SHYXIN

@SHYXIN SHYXIN commented May 17, 2026

Copy link
Copy Markdown
Contributor

Summary

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.

Changes

  • starlette_admin/base.py — Added _get_default_values() method that reads column.default.arg from the model's __table__
  • _render_create() — On GET requests, calls _get_default_values() and passes the result as obj to the template context

How it works

For SQLAlchemy models, the method iterates over model.__table__.columns and extracts col.default.arg for 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

  • All 14 views tests pass
  • Manual verification with SQLAlchemy model defaults

Additional

Includes chore commit to fix pyproject.toml pytest config conflict.

SHYXIN added 2 commits May 17, 2026 17:37
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

codecov Bot commented Jun 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (0f684fe) to head (19a2ece).
⚠️ Report is 2 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant