amisadmin / fastapi-amis-admin

FastAPI-Amis-Admin is a high-performance, efficient and easily extensible FastAPI admin framework. Inspired by django-admin, and has as many powerful functions as django-admin.
http://docs.amis.work
Apache License 2.0
1.05k stars 154 forks source link

当模型字段设置了regex,在ModelAdmin类的search_fields中添加该字段,会引发fastapi.exceptions.RequestValidationError #80

Open ClanEver opened 1 year ago

ClanEver commented 1 year ago

Model

class User(MyBaseModel, table=True):
    username: str = Field(max_length=32, index=True, unique=True,
                          regex='^[0-9A-Za-z]{5,30}$'
                          )
    password: bytes = Field(default=None)
    salt: bytes = Field(default=None)
    ...

ModelAdmin

class UserAdmin(admin.ModelAdmin):
    group_schema = None
    page_schema = PageSchema(label="用户", icon="fa fa-folder")
    model = User
    search_fields = [User.username]

Error

fastapi.exceptions.RequestValidationError: 1 validation error for Request
body -> username
  string does not match regex "^[0-9A-Za-z]{5,30}$" (type=value_error.str.regex; pattern=^[0-9A-Za-z]{5,30}$)

当我删掉regex或者search_fields后恢复正常 目测是获取列表的请求中对字段进行了验证 image

Full Code ```python from fastapi import FastAPI from fastapi_amis_admin import admin from fastapi_amis_admin.admin import AdminApp from fastapi_amis_admin.admin.settings import Settings from fastapi_amis_admin.admin.site import AdminSite from fastapi_amis_admin.amis import PageSchema from sqlmodel import Field, SQLModel, create_engine engine = create_engine(url="sqlite:///amisadmin.db", echo=False) class User(SQLModel, table=True): id: int = Field(primary_key=True) username: str = Field(max_length=32, index=True, unique=True, regex='^[0-9A-Za-z]{5,30}$') password: bytes = Field(default=None) salt: bytes = Field(default=None) def create_db_table(): SQLModel.metadata.create_all(engine) create_db_table() # Create FastAPI application app = FastAPI() # Create AdminSite instance site = AdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///amisadmin.db', root_path='/')) @site.register_admin class AuthApp(admin.AdminApp): page_schema = PageSchema(label="用户管理", icon="fa fa-users") router_prefix = "/eimusic_auth" def __init__(self, app: "AdminApp"): super().__init__(app) self.register_admin(UserAdmin) class UserAdmin(admin.ModelAdmin): group_schema = None page_schema = PageSchema(label="用户", icon="fa fa-folder") model = User search_fields = [User.username] # Mount the background management system site.mount_app(app) if __name__ == '__main__': import uvicorn uvicorn.run(app) ```
amisadmin commented 1 year ago

是的, 查询的时候会默认添加一个字段筛选操作符前缀, 后期可能会优化筛选, 但是目前更换这部分逻辑工作量会比较大.

tomasky commented 1 year ago

textchoices 和 integerchoices 也会引发这个。可以添加一个标志位,为True,就是非模糊搜索,不添加前缀。