Skip to main content

Generic FastAPI + SQLAlchemy admin backend.

Project description

xladmin backend

xladmin-backend is the backend package published to PyPI as xladmin.

Important:

  • package name on PyPI: xladmin
  • Python import: from xladmin import ...
  • monorepo: Artasov/xladmin

Public API

  • AdminConfig / ModelConfig / FieldConfig
  • ListFilterConfig
  • BulkActionConfig / ObjectActionConfig
  • ModelsBlock
  • HttpConfig
  • create_router(...)

Compatibility aliases are kept:

  • Admin* config names
  • create_admin_router(...)

Minimal Example

from xladmin import AdminConfig, HttpConfig, ModelConfig, create_router

from src.core.auth.dependencies import get_current_user
from src.core.db.session import get_db_session
from src.modules.identity.models import UserORM


config = AdminConfig(
    models=(
        ModelConfig(model=UserORM),
    ),
)

router = create_router(
    HttpConfig(
        registry=config,
        get_db_session_dependency=get_db_session,
        get_current_user_dependency=get_current_user,
        is_allowed=lambda user: bool(user.is_staff),
    ),
)

ModelConfig(model=UserORM) is enough for a basic admin. The library derives default slug, title, search fields, and ordering from the ORM model.

Features

  • list / detail / create / patch / delete endpoints
  • bulk actions and object actions
  • relation choices and relation filters
  • single and multi-select relation filters via ListFilterConfig(..., multiple=True, input_kind="relation-multiple")
  • overview metadata and model blocks
  • query_for_list and custom search_query_builder
  • mode-specific form fields with hidden_in_create / hidden_in_update
  • custom create defaults with create_item_factory
  • delete preview for single and bulk delete
  • RU / EN locale metadata for the frontend

Multi-Select Relation Filters

If one relation filter is not enough, you can expose a multi-select variant that works as an autocomplete with add/remove chips on the frontend.

from xladmin import ListFilterConfig


ListFilterConfig(
    slug="role_ids",
    label="Roles",
    field_name="roles",
    input_kind="relation-multiple",
    multiple=True,
    relation_model=RoleORM,
    relation_label_field="name",
)

Create And Update Fields

If a field should be visible only in one form mode, use hidden_in_create or hidden_in_update.

from xladmin import FieldConfig, ModelConfig


ModelConfig(
    model=UserORM,
    fields={
        "password": FieldConfig(
            input_kind="password",
            hidden_in_update=True,
            value_setter=set_user_password,
        ),
        "new_password": FieldConfig(
            input_kind="password",
            hidden_in_create=True,
            value_getter=lambda _user: "",
            value_setter=set_user_password,
        ),
    },
)

If create requires hidden service fields, use create_item_factory.

from xladmin import ModelConfig


def create_admin_user(payload, session, user):
    del payload, session, user
    return UserORM(
        date_joined=AuthBase.now(),
        secret_key=AuthBase.generate_secret_key(),
    )


ModelConfig(
    model=UserORM,
    create_fields=("username", "email", "password"),
    create_item_factory=create_admin_user,
)

If create needs a fully custom form and payload handler, use create_form + create_handler.

The same FormFieldConfig mechanism is also available for object_actions and bulk_actions via form=.... If an action has no form, the frontend runs it immediately as before.

For datetime inputs the built-in dialog uses the MUI action bar with a localized Today / Сегодня button, which inserts the current date and time.

from xladmin import FormFieldConfig, FormFieldOptionConfig, ModelConfig


async def create_proxy(session, model_config, payload, user):
    del session, model_config, user
    parsed = ProxyBase.parse_raw(f"{payload['scheme']}://{payload['proxy']}")
    return ProxyORM(
        name=parsed.name,
        scheme=parsed.scheme,
        host=parsed.host,
        port=parsed.port,
        username=parsed.username,
        password=parsed.password,
        created_at=ProxyBase.now(),
        updated_at=ProxyBase.now(),
    )


ModelConfig(
    model=ProxyORM,
    create_form=(
        FormFieldConfig(
            name="scheme",
            label="Scheme",
            input_kind="select",
            required=True,
            options=(
                FormFieldOptionConfig(value="http", label="HTTP"),
                FormFieldOptionConfig(value="socks5h", label="SOCKS5H"),
            ),
        ),
        FormFieldConfig(
            name="proxy",
            label="Proxy",
            placeholder="login:password@ip:port",
            required=True,
        ),
    ),
    create_handler=create_proxy,
)

Compatibility

  • FastAPI >=0.115,<1.0
  • Pydantic >=2.9,<3.0
  • SQLAlchemy >=2.0,<3.0
  • Python >=3.12

Development

uv sync --extra dev
uv run pytest
uv run ruff check .
uv run mypy
uv run python -m build
uv run python -m twine check dist/*

Docs

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xladmin-0.4.0.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xladmin-0.4.0-py3-none-any.whl (30.0 kB view details)

Uploaded Python 3

File details

Details for the file xladmin-0.4.0.tar.gz.

File metadata

  • Download URL: xladmin-0.4.0.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for xladmin-0.4.0.tar.gz
Algorithm Hash digest
SHA256 bfdacaa57f1b90865c72bfa7b61d9469451f65c9de93ecf35b81354b1dbe4b27
MD5 0e3244027e9d821485c72c5c27812ae2
BLAKE2b-256 968166234477dd455e47687b9a8b9f448052b4fa7c43b6e7694e916519cfde55

See more details on using hashes here.

File details

Details for the file xladmin-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: xladmin-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 30.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for xladmin-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aad2349e9d3120ee125a9aa4777aade9df9b7f95006983abacf214bd5ee61119
MD5 6078546ed90b4fb7d7bfe74678f1d180
BLAKE2b-256 8304744faa3b0a0dcc070c5f9ae2be1064a88339c83abd4d2c88f9b1c1958c8a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page