Skip to main content

sqladmin-litestar-plugin

Overview

The SQLAdminPlugin integrates SQLAdmin with a Litestar application.

Acknowledgements

Thanks to aminalaee and all contributors for the excellent SQLAdmin project.

Installation

To install the dependencies, run:

pip install sqladmin-litestar-plugin

Usage

To use the plugin, import the SQLAdminPlugin class and pass it to the Litestar application.

By default, the plugin will create a new admin interface at /admin.

Example

from litestar import Litestar
from sqladmin import ModelView
from sqladmin_litestar_plugin import SQLAdminPlugin
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import declarative_base


engine = create_async_engine("sqlite+aiosqlite:///example.db")
Base = declarative_base()


class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String)


class UserAdmin(ModelView, model=User):
    column_list = [User.id, User.name]


async def on_startup() -> None:
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)  # Create tables


admin = SQLAdminPlugin(views=[UserAdmin], engine=engine)
app = Litestar(plugins=[admin], on_startup=[on_startup])

Configuration

The SQLAdminPlugin accepts the following arguments:

  • views: A sequence of ModelView classes to add to the admin app. Each ModelView class configures the interface for a SQL model.
  • engine: An SQLAlchemy engine to connect to your database.
  • sessionmaker: An SQLAlchemy sessionmaker instance used to manage sessions.
  • base_url: The base URL where the admin app will be hosted.
  • title: The title of the admin app, which appears in the browser's title bar and the header of the admin interface.
  • logo_url: The URL of the logo to display in the admin app, enhancing brand visibility.
  • templates_dir: The directory containing the Jinja2 templates for the admin interface, allowing for customization of the UI.
  • middlewares: A sequence of Starlette middlewares to add to the admin app, useful for handling requests or adding additional functionality.
  • authentication_backend: An authentication backend to secure the admin app, managing user authentication and authorization.

Views are not added to the admin app until the Litestar application is instantiated, so you can append views to the views list until this point.

Use with Advanced-Alchemy Audit Base Variants

Advanced-Alchemy (AA) provides variants of base models that include created_at and updated_at fields which enforce that tzinfo is set on the datetime instance passed through to SQLAlchemy.

When a model is created via the SQLAdmin UI, the created_at and updated_at fields default to the current time in UTC, however, the tzinfo property of the datetime is not set.

sqladmin-litestar-plugin provides a custom ModelView class that ensures the tzinfo property is set on datetime instances when the form field represents an AA DateTimeUTC field.

Example:

from __future__ import annotations

from advanced_alchemy.base import UUIDAuditBase
from sqlalchemy import Column, String

from sqladmin_litestar_plugin.ext.advanced_alchemy import AuditModelView


class Entity(UUIDAuditBase):
    my_column = Column(String(10))


class EntityAdmin(AuditModelView, model=Entity): ...

For a full working example, see the examples/aa_audit_base directory in this repo.

The AuditModelView class should also be useful for models that don't depend on one of the AA audit model bases, but still use DateTimeUTC fields.

Metadata

Release files for sqladmin-litestar-plugin 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqladmin-litestar-plugin 0.2.0
File Size Uploaded
sqladmin_litestar_plugin-0.2.0.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqladmin-litestar-plugin 0.2.0
File Interpreter ABI Platform
sqladmin_litestar_plugin-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / sqladmin_litestar_plugin-0.2.0.tar.gz

Download URL sqladmin_litestar_plugin-0.2.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fb0bbc64812528324cd753738788a95ee298cc33066e9382646ecd1995a61fff
BLAKE2b-256 checksum
How to use checksums
a98990fdb698565857b79c3cc84058c705acab75cfe19ec8cfd6c82ffb15fb3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / sqladmin_litestar_plugin-0.2.0-py3-none-any.whl

Download URL sqladmin_litestar_plugin-0.2.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07e6d5a3f996e8618c305f5401d2040375617453bd08314132bb028295341ecf
BLAKE2b-256 checksum
How to use checksums
8ce79398b0a4b162b69a311241acb2370c705c5d067cd1af72375efc617d1770
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page