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.09k stars 161 forks source link

SqlAlchemy model with UUID column does not work properly #166

Open vsipchenko opened 7 months ago

vsipchenko commented 7 months ago

I have the next model

class Base(DeclarativeBase):
    __abstract__ = True
    id = Column(UUID(as_uuid=True), primary_key=True, index=True, unique=True, server_default=text("gen_random_uuid()"))

class User(Base):
    __tablename__ = "users"
    __pydantic_model__ = UserSchema

class UserAdmin(ModelAdmin):
    model = User

When i register this UserAdmin class i am able to view list of users in admin screen, but I can not update them, I get an exception:

hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'UUID' object has no attribute 'replace'

For me it seems that function get_python_type_parse which is used in SqlalchemyCrud class does not handle UUID type, and in method _fetch_item_scalars it tries to pass UUID instance into UUID class

can you resolve this issue by adding UUID handler to get_python_type_parse ?

def get_python_type_parse(field: Union[InstrumentedAttribute, Column, Label]) -> Callable:
    try:
        python_type = field.expression.type.python_type
        if issubclass(python_type, UUID):
            return str
        if issubclass(python_type, datetime.date):
            if issubclass(python_type, datetime.datetime):
                return parse_datetime
            return parse_date
        return python_type
    except NotImplementedError:
        return str