Skip to content

TypeError: UUID objects are immutable" when try view, delete, update user from fastapi-users in sqladmin panel #668

@ghost

Description

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

Hi! I faced with a problem
I want to view, edit or delete an instance of the User model in the admin panel, but I got that error: TypeError: UUID objects are immutable

Steps to reproduce the bug

models.py

class User(SQLAlchemyBaseUserTableUUID, Base):
    is_active: Mapped[bool] = mapped_column(nullable=True, default=False)
    tariff: Mapped[str] = mapped_column(String, ForeignKey('tariff.slug', name='fk_user_tariff'), nullable=True)

    advert_tokens: Mapped[list[AdvertToken]] = relationship(
        secondary='m2m_advert_token_for_user', back_populates='users', lazy='selectin'
    )

    user_permissions: Mapped[list['Permissions']] = relationship(
        secondary='m2m_user_permissions', back_populates='users', lazy='selectin'
    )

    auto_adverts: Mapped[list[AutoAdvert]] = relationship(lazy='selectin')
    external_resources: Mapped[list['SyncLink']] = relationship(back_populates='user', cascade='all, delete')

admin_models.py

class UserAdmin(ModelView, model=User):

    model = User

    pk_columns = [
        model.id
    ]

    column_list = [
        User.email,
        User.id,
        User.tariff,
        User.is_active,
        User.is_superuser,
        User.is_verified
    ]
    can_create = True
    can_edit = True
    can_delete = True
    # can_view_details = False
    #
    # name = 'User'
    # name_plural = 'Users'

    category = 'User'

Try to view, delete or edit an instance of User model in the admin panel

Expected behavior

You can do smth with User model

Actual behavior

TypeError: UUID objects are immutable

Debugging material

Traceback (most recent call last):
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 435, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/fastapi/applications.py", line 292, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/base.py", line 109, in __call__
    await response(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/responses.py", line 270, in __call__
    async with anyio.create_task_group() as task_group:
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 597, in __aexit__
    raise exceptions[0]
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/responses.py", line 273, in wrap
    await func()
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/base.py", line 134, in stream_response
    return await super().stream_response(send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/responses.py", line 262, in stream_response
    async for chunk in self.body_iterator:
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/base.py", line 98, in body_stream
    raise app_exc
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/prometheus_fastapi_instrumentator/middleware.py", line 169, in __call__
    raise exc
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/prometheus_fastapi_instrumentator/middleware.py", line 167, in __call__
    await self.app(scope, receive, send_wrapper)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/cors.py", line 91, in __call__
    await self.simple_response(scope, receive, send, request_headers=headers)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/cors.py", line 146, in simple_response
    await self.app(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 443, in handle
    await self.app(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/sessions.py", line 86, in __call__
    await self.app(scope, receive, send_wrapper)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
               ^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/sqladmin/authentication.py", line 66, in wrapper_decorator
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/sqladmin/application.py", line 482, in delete
    model = await model_view.get_object_for_delete(pk)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/sqladmin/models.py", line 839, in get_object_for_delete
    stmt = self._stmt_by_identifier(value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/sqladmin/models.py", line 845, in _stmt_by_identifier
    values = object_identifier_values(identifier, self.model)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tailo/Library/Caches/pypoetry/virtualenvs/app-rGgr4MIE-py3.11/lib/python3.11/site-packages/sqladmin/helpers.py", line 234, in object_identifier_values
    values.append(get_column_python_type(pk)(part))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py", line 1263, in __call__
    result.__orig_class__ = self
    ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/uuid.py", line 278, in __setattr__
    raise TypeError('UUID objects are immutable')
TypeError: UUID objects are immutable

изображение

Environment

macOS sonoma 14.0 / python 3.11 / sqladmin 0.15.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions