fastapi-amis-admin

repository·master·Indexed 23 days ago

https://github.com/amisadmin/fastapi-amis-admin

A high-performance, extensible admin framework for FastAPI inspired by Django-admin. It utilizes the Amis front-end library to automatically generate visual dashboards and CRUD interfaces based on Python type hints and database models. It supports SQLModel, SQLAlchemy, and SQLAlchemy 2.0, and includes a CLI tool (faa) for project and application initialization.

Tokens
28.2K
Snippets
69
Records
130
Agent score
81%

What's inside fastapi-amis-admin

  1. Understand ModelAdmin data control flow

    master

    The ModelAdmin manages data through four main lifecycle stages: Read, Create, Update, and List. The final configuration is determined by a priority system where explicit overrides (like get_list_display) take precedence over field definitions.

    Data Flow Summary

    • Read: Uses schema_read to produce ReadApiResponse. Field selection is driven by fields and exclude.
    • Create: Uses schema_create and get_create_form to produce AmisCreateForm, which is then sent to the route_create endpoint.
    • Update: Uses schema_update and get_update_form to produce AmisUpdateForm. Fields can be restricted via update_fields or made readonly_fields.
    • List: Combines fields, list_display, and list_filter to build the TableCRUD view. Search functionality is driven by search_fields and get_list_filter_form.
  2. Understand the BaseAdmin class and its attributes

    master

    The BaseAdmin class serves as the fundamental base class for page management within fastapi-amis-admin. When working with admin objects, you can access the following attributes:

    • app: The AdminApp object that the current admin object is registered to. Note that app is not necessarily the top-level object and might be registered by another AdminApp or AdminSite.
    • site: The AdminSite instance where the current management object is registered (the top-level Admin object).
    • unique_id: A unique identifier for the management object.
      • You can customize this setting.
      • If not provided, it is automatically generated.
      • Requirement: Unique IDs must remain consistent when a project starts or stops and must be unique per Admin class within a single project.
  3. Understand the AdminAction base class

    master

    The AdminAction class serves as the base class for defining administrative actions within the fastapi-amis-admin framework. It provides the necessary context for an action to interact with the ModelAdmin and the Amis UI components.

    Key Fields

    • admin: The ModelAdmin object that the action belongs to.
    • action: The Amis Action object representing the UI behavior.
    • name: A unique identifier for the action. This must be unique to prevent overwriting previous actions.
    • label: The display name shown in the UI.
    • flags: Determines where the action appears in the ModelAdmin interface. Use these to control visibility:
      • item: Displayed on every row in a list.
      • bulk: Displayed in the bulk operations menu.
      • toolbar: Displayed in the list toolbar.
      • column: Displayed in the last column of the list.
    • getter: A method used to retrieve the Action object.
  4. Configure SQLModelSelector for queries

    master

    The SQLModelSelector is used to select and filter fields from a SQLModel ORM model. When configuring a selector, you can specify which fields to include, which to exclude, and how to order them.

    Key configuration options include:

    • model: The SQLModel ORM model (required).
    • fields: A list of fields to query. Supports current model fields or fields from other models. Defaults to self.model.
    • exclude: A list of fields to exclude from the current model.
    • ordering: A list of selectors to sort the field list.
    • pk_name: The primary key string of the current model (defaults to id).
    • pk: The SQLAlchemy InstrumentedAttribute for the primary key.
  5. Explore the FastAPI-Amis-Admin class hierarchy

    master

    The project uses a structured inheritance hierarchy to manage different types of admin interfaces. Key relationships include:

    • Core Admin Types:
      • BaseAdmin: The root for page management.
      • PageSchemaAdmin: Adds page_schema to BaseAdmin.
      • RouterAdmin: Adds an APIRouter via RouterMixin.
      • PageAdmin: Combines PageSchemaAdmin and RouterAdmin, adding a page attribute.
    • Specialized Admin Types:
      • FormAdmin: Inherits from BaseActionAdmin, adding a form and a BaseModel schema.
      • ModelAdmin: The primary class for database management, inheriting from BaseActionAdmin and SQLModelCrud.
      • TemplateAdmin: Adds Jinja2Templates support.
    • Action & CRUD Types:
      • AdminAction: The base for actions, containing an action attribute.
      • FormAction: Inherits from AdminAction and BaseActionAdmin.
      • SQLModelCrud: Provides CRUD capabilities using SQLModel.
  6. Use ModelAdmin to manage models

    master

    The ModelAdmin class is the primary way to manage model administration. You can inherit from BaseModelAdmin or specialized classes like PageAdmin or SQLModelCrud to implement management logic.

    One key feature is bind_model. When bind_model=True (the default), the management page is automatically discoverable via AdminSite.get_model_admin. This is particularly useful for foreign key associations, as the default FormItem (using TablePicker) will automatically use the corresponding management page for the bound model.

  7. How permission control hierarchy works

    master

    FastAPI-Amis-Admin uses a hierarchical permission model that flows from the top-level AdminSite down to specific ModelAdmin actions.

    1. AdminSite: The highest level. If has_page_permission fails here, the entire admin site is inaccessible.
    2. AdminApp: Controls groups of administrative objects. If has_page_permission fails here, all objects registered under this app are hidden.
    3. ModelAdmin / PageAdmin: Controls specific data models or pages.
      • If has_page_permission fails for a ModelAdmin, the menu item is hidden and all its routes are disabled.
      • If has_page_permission passes, the system then checks granular CRUD permissions (has_list_permission, has_create_permission, etc.).

    If any permission check fails, the system automatically disables the corresponding UI elements (like menu items or action buttons) and disables the corresponding API routes.

  8. Use AdminSite for a complete administrative experience

    master

    While BaseAdminSite provides the core functionality, AdminSite is a specialized subclass designed for full-featured administrative sites. Unlike the base class, AdminSite automatically registers several administrative classes by default, including:

    • HomeAdmin
    • DocsAdmin
    • ReDocsAdmin
    • FileAdmin

    Use AdminSite when you want a ready-to-use management interface with built-in documentation and file management capabilities.