jonra1993 / fastapi-alembic-sqlmodel-async

This is a project template which uses FastAPI, Pydantic 2.0, Alembic and async SQLModel as ORM. It shows a complete async CRUD using authentication and role base access control.
MIT License
878 stars 142 forks source link

Many to many data insert #86

Closed russell310 closed 8 months ago

russell310 commented 9 months ago

I want to set role, groups at a single post request. My payload is: { "firstName": "string", "lastName": "string", "email": "user1@example.com", "isActive": true, "isSuperuser": false, "status": false, "roleId": "ff36d9f0-1ace-43ed-b164-d941f5312568", "password": "string", "groups": ["419bd58a-ec64-4807-b227-9dadff3c405c"] } How to achieve this?

FYI: I'm solving in this way?

   async def create_with_role(
            self, *, obj_in: UserCreate, db_session: AsyncSession | None = None
    ) -> User:
        db_session = db_session or super().get_db().session
        db_obj = User.from_orm(obj_in)
        db_obj.hashed_password = get_password_hash(obj_in.password)
        if obj_in.groups:
            groups_list = await db_session.execute(
                select(Group).where(Group.id.in_(obj_in.groups))
            )
            groups = [item[0] for item in groups_list]
            db_obj.groups.extend(groups)
        db_session.add(db_obj)
        await db_session.commit()
        await db_session.refresh(db_obj)
jonra1993 commented 9 months ago

Hello @russell310 I think to do this you need to create a custom IUserCreateWithGroups and create a custom crud as your last code if you want to create in one step another alternative is to do that in two steps create the user and after that update user groups whith this crud add_user_to_group. What was your result with the above code?

russell310 commented 9 months ago

Hello @russell310 I think to do this you need to create a custom IUserCreateWithGroups and create a custom crud as your last code if you want to create in one step another alternative is to do that in two steps create the user and after that update user groups whith this crud add_user_to_group. What was your result with the above code?

My code works fine. But stuck in added extra field with linkmodel. Any solution? Like if I've payload like this:

{
    "firstName": "string",
    "lastName": "string",
    "email": "user1@example.com",
    "isActive": true,
    "isSuperuser": false,
    "status": false,
    "roleId": "ff36d9f0-1ace-43ed-b164-d941f5312568",
    "password": "string",
    "phone": "67213128",
    "enterprises": [{
            "id": "419bd58a-ec64-4807-b227-9dadff3c4051",
            "active": true
        }]
}
jonra1993 commented 9 months ago

My recommendation in that case is for the handler to do that in two steps instead of in one create a custom schema with the format you want to create like ICreateUserWithEnterprises when you receive data create an IUserCreate using income schema and create the user and after that use enterprises data to update information of which enterprises a user is part of.