sqladmin icon indicating copy to clipboard operation
sqladmin copied to clipboard

custom action in example not working

Open akudryav opened this issue 10 months ago • 4 comments

Checklist

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

Describe the bug

In example https://aminalaee.dev/sqladmin/configurations/#custom-action function get_object_for_edit is called with param pk but definition waits for request object: async def get_object_for_edit(self, request: Request)

and it looks for for path_param 'pk' in request not 'pks'

Steps to reproduce the bug

Use example in https://aminalaee.dev/sqladmin/configurations/#custom-action

Expected behavior

No response

Actual behavior

Error

Debugging material

No response

Environment

any/3.12/last

Additional context

No response

akudryav avatar Feb 26 '25 14:02 akudryav

I've had the same problem

sdhou avatar Mar 10 '25 02:03 sdhou

I am facing the same problem. The solution for me was to call self.session_maker() in the context manager and use my repo to get the object instead of using the get_object_for_edit() function:

async def reset_password(self, request: Request):
    pks = request.query_params.get("pks", "").split(",")
    if pks:
        for pk in pks:
            async with self.session_maker() as session:
                model = await user_crud.get_one(session, int(pk))
                ...

I think this situation is related to this accepted pull-request

The edit_form_query implementation tries to analyze path_params to get pk, while the documentation tells us to pass pk from the pks contained in query_params there.

AlKorochkin avatar Mar 11 '25 16:03 AlKorochkin

I used function get_object_for_details() instead of get_object_for_edit()

akudryav avatar Mar 11 '25 17:03 akudryav

Hi there! Same here. I solved it implementing a new method on my ModelView:

    async def get_object(self, value):
        stmt = self._stmt_by_identifier(value)
        return await self._get_object_by_pk(stmt)

And calling it when needed. I've prefered that rather than overriding existing base functionalities because I can customize the method without touching shared logic.

simon1494 avatar Jun 20 '25 14:06 simon1494