Model management is the most commonly used management class in background management, and its functions are also the most abundant. fastapi-amis-admin has implemented various basic operations commonly used for data models, and you can still make on this basis. More personalized extensions.
# First create a SQLModel model, please refer to: https://sqlmodel.tiangolo.com/ class Category(SQLModel, table=True): id: int = Field(default=None, primary_key=True, nullable=False) name: str = Field(title='CategoryName') description: str = Field(default='', title='Description') # Register ModelAdmin @site.register_admin class CategoryAdmin(admin.ModelAdmin): page_schema = 'Category Management' # Configuration management model model = Category
Let's take a look at this simple example-1. It completes the following steps:
- Define a
- Define a model management class that inherits
- Configure the model and register to the management site.
About the SQLModel model
In fact, this part of the code does not belong to the
amis-admin code, because it can be reused anywhere ORM mapping is required, in the project you should define a separate
models.py file to write this part of the code.
SQLModel is a very good Python ORM library, written by the same author of FastAPI, which perfectly combines SQLAlchemy and Pydantic. Please read its official documentation: https://sqlmodel.tiangolo.com/
# Create a SQLModel model, please refer to: https://sqlmodel.tiangolo.com/ class Article(SQLModel, table=True): id: int = Field(default=None, primary_key=True, nullable=False) title: str = Field(title='ArticleTitle', max_length=200) description: Optional[str] = Field(default='', title='ArticleDescription', max_length=400) status: bool = Field(None, title='status') content: str = Field(title='ArticleContent') # Associate Category model, model definition reference [Example-1] category_id: Optional[int] = Field(default=None, foreign_key="category.id", title='CategoryId') # category: Optional[Category] = Relationship(back_populates="articles") @site.register_admin class ArticleAdmin(admin.ModelAdmin): page_schema = 'article management' model = Article # Set the fields to display list_display = [Article.id, Article.title, Article.description, Article.status, Category.name] # Set up fuzzy search field search_fields = [Article.title, Category.name] # custom base selector async def get_select(self, request: Request) -> Select: stmt = await super().get_select(request) return stmt.outerjoin(Category, Article.category_id == Category.id)
Example 2 is more complicated than Example 1. But if you are familiar with
Django-Admin you will find they are very similar, yes.
fastapi_amis_admin is inspired by
Django-Admin, so many functions are designed with is similar, and
The functions are more abundant and the configuration is more flexible.
Configure display fields¶
By default, the management list will display all fields of the current model. But if you only need to display some fields, or you also need to display other fields of related models, you can customize the fields to be displayed by configuring
For example in example 2:
Article.category_idare not included in the display field.
Category.nameis a field of another model and can also be included in the display field, but here you need to customize the selector
Categorymodel through the
Configure fuzzy search fields¶
search_fields configures the query method for string fields as
like filtering. If
search_fields is not configured, the default is
equal query method.
get_select custom selector can achieve different initial data query conditions for different requests, similar to the
get_queryset method in
Currently, the model management class
fastapi-amis-admin has supported functions including but not limited to the following list.
|Function||Related Fields or Methods||Remarks|
|Custom bulk query fields||
|Custom batch query display fields||
||Support display types are picture, audio, video, list,
|Custom batch query filter form||
||Support text exact/fuzzy matching, time range filtering, multiple selection filtering|
|Custom batch query sorting field||
|Custom model primary key||
|Custom bulk query selector||
|Custom Model Database||
|Custom batch query read-only fields||
|Customize batch query data volume per page||
|Custom bulk query fuzzy search fields||
|Customize fields for new models||
|Customize fields that support editing||
|Customize fields that support bulk editing||
|Customize the new model data form||
|Custom update model data form||
|Customize the new model data execution action||
|Custom update model data execution action||
|Custom delete model data execution action||
|Custom batch query data return protocol||
|Customize batch query data filtering submission protocol||
|Custom Create Data Submission Protocol||
|Custom read data return protocol||
|Custom update data submission protocol||
|Custom batch query permission||
|Custom single query permission||
|Custom create data permission||
|Custom update data permission||
|Custom delete data permission||
The usage of
ModelAdmin is very flexible, only the most basic usage is shown here, you can read the API documentation
Or refer to the demo program for more detailed usage. Examples of specific application scenarios will be added in the future.
If you have better application examples or tutorial documents, you can submit them through github, thank you very much for your support!
fastapi_amis_admin will do better!
About fastapi_amis_admin and django-admin
django-admin is a very mature and powerful web management background tool, users who use django should often use it, But he is not suitable for non-django projects, which is one of the main reasons why fastapi_amis_admin was born.
Fastapi_amis_admin has more extensions and functions than django-admin, but fastapi_amis_admin is still in the growth stage, many functions are immature, and need long-term continuous improvement and upgrading. I look forward to your participation in fastapi_amis_admin, contribute code to the project, or provide suggestions.