Skip to content

Add SQLModel backend based on SQLAlchemy ModelView#2898

Open
Nefelibata0516 wants to merge 6 commits into
pallets-eco:masterfrom
Nefelibata0516:feat/sqlmodel-support-2528
Open

Add SQLModel backend based on SQLAlchemy ModelView#2898
Nefelibata0516 wants to merge 6 commits into
pallets-eco:masterfrom
Nefelibata0516:feat/sqlmodel-support-2528

Conversation

@Nefelibata0516
Copy link
Copy Markdown

@Nefelibata0516 Nefelibata0516 commented May 16, 2026

Add SQLModel backend support with minimal CRUD integration

Background

This PR implements Issue #2528 by adding SQLModel support to Flask-Admin.
SQLModel is built on top of SQLAlchemy ORM, so the implementation reuses Flask-Admin’s existing SQLAlchemy backend instead of duplicating CRUD/query logic.

Scope

This PR targets a minimal viable integration:

  • list view rendering
  • create/edit/delete form workflow
  • basic CRUD behavior with SQLModel models
    Out of scope for this PR:
  • advanced filters
  • complex relationship customizations
  • file upload and other advanced backend-specific features

Changes

1) New SQLModel backend

  • Added flask_admin.contrib.sqlmodel module:
    • flask_admin/contrib/sqlmodel/view.py
    • flask_admin/contrib/sqlmodel/__init__.py
  • Added SQLModelView and ModelView alias for ergonomic usage.

2) Compatibility layer for SQLModel-specific types

  • Reused flask_admin.contrib.sqla.ModelView as base class.
  • Added type-name normalization so SQLModel AutoString works with Flask-Admin’s SQLAlchemy converters:
    • form converter normalization (AutoString -> String)
    • filter converter normalization (AutoString -> string)

3) Tests

  • Added basic SQLModel CRUD test:
    • flask_admin/tests/sqlmodel/test_basic.py
  • Covers:
    • list page accessible
    • create model
    • edit model
    • delete model

4) Example app

  • Added runnable SQLModel example app under:
    • examples/sqlmodel/
  • Demonstrates practical integration with Flask-Admin.

5) CI/stability follow-up fixes

  • Added SQLModel to test dependency group to avoid CI import errors.
  • Addressed typing/style issues related to SQLModel examples and backend adapter.

Why this design

  • SQLModel uses SQLAlchemy under the hood, so inheriting from SQLAlchemy ModelView is the lowest-risk and most maintainable approach.
  • The adapter layer is intentionally small and focused on compatibility gaps, keeping behavior consistent with existing SQLAlchemy backend semantics.

Validation

Local verification

  • uv run pytest flask_admin/tests/sqlmodel/test_basic.py
  • SQLModel example app runs and /admin/ is accessible ✅

CI

  • All CI checks pass for this PR branch ✅

Notes

This PR intentionally delivers a minimal, demonstrable integration first.
Advanced SQLModel-specific enhancements can be added incrementally in follow-up PRs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this file needed?

@ElLorans
Copy link
Copy Markdown
Contributor

Thanks for you PR! A few considerations:

  1. The changes are 50 LoCs in contrib and 80 in tests. Do we really need 1400 LoCs in examples?
  2. Rather than creating new tests, should we parametrize the ones for sqlalchemy?
  3. Can you take a very short look at https://github.com/pallets-eco/flask-admin/pull/2651/changes which tried to add support for sqlmodel

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.

2 participants