Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using multi_joined with response object in object #52

Closed
vnlebaoduy opened this issue Apr 15, 2024 · 8 comments · Fixed by #56
Closed

Using multi_joined with response object in object #52

vnlebaoduy opened this issue Apr 15, 2024 · 8 comments · Fixed by #56
Assignees
Labels
enhancement New feature or request FastCRUD Methods Related to FastCRUD methods

Comments

@vnlebaoduy
Copy link

vnlebaoduy commented Apr 15, 2024

@igorbenav I have more a question about get multi_joined. When i joined 2 table many to one. I can't not create response object in object. Example: I only create like that

"data": [
    {
      "id": 0,
      "title": "string",
      "template_id": 0,
      "description": "string",
      "created_at": "2024-04-14T16:28:01.403Z",
      "created_by": "string",
      "template_title": "Example text for encryption",
      "template_description": "Example description for encryption template"
    }
  ]

This is response that i want

"data": [
    {
      "id": 0,
      "title": "string",
      "template_id": 0,
      "description": "string",
      "created_at": "2024-04-14T16:28:01.403Z",
      "created_by": "string",
      "created_at": "2024-04-14T16:28:01.403Z",
      "created_by": "string",
      "template":{
              "title": "Example text for encryption",
              "description": "Example description for encryption template"
       }
    }
  ]

Please help me :(

@igorbenav
Copy link
Owner

igorbenav commented Apr 15, 2024

Could you please also provide the SQLAlchemy table definitions and the exact code that generated this output? Plus the fastcrud version.

@vnlebaoduy
Copy link
Author

vnlebaoduy commented Apr 15, 2024

Could you please also provide the SQLAlchemy table definitions and the exact code that generated this output? Plus the fastcrud version.

This is my table

class plan(Base):
    __tablename__ = "plan"

    id: Mapped[int] = mapped_column("id", autoincrement=True, nullable=False, unique=True, primary_key=True, init=False)
    title: Mapped[str] = mapped_column(String(30))
    template_id: Mapped[int] = mapped_column(ForeignKey("encrypt_template.id"))
    description: Mapped[str|None] = mapped_column(String, default=None)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default_factory=lambda: datetime.now(UTC))
    created_by: Mapped[str | None] = mapped_column(String,default=None)
    updated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None)
    updated_by: Mapped[str| None] = mapped_column(String, default=None)
    deleted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None)
    deleted_by: Mapped[str| None] = mapped_column(String, default=None)
    is_deleted: Mapped[bool] = mapped_column(default=False, index=True)
class EncryptTemplate(Base):
    __tablename__ = "encrypt_template"

    id: Mapped[int] = mapped_column("id", autoincrement=True, nullable=False, unique=True, primary_key=True, init=False)
    title: Mapped[str] = mapped_column(String)
    description: Mapped[str] = mapped_column(String)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default_factory=lambda: datetime.now(UTC))
    created_by: Mapped[str | None] = mapped_column(String,default=None)
    updated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None)
    updated_by: Mapped[str| None] = mapped_column(String, default=None)
    deleted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None)
    deleted_by: Mapped[str| None] = mapped_column(String, default=None)
    is_deleted: Mapped[bool] = mapped_column(default=False, index=True)

@vnlebaoduy
Copy link
Author

@igorbenav How can i fix this

@igorbenav
Copy link
Owner

Hey, @vnlebaoduy, which version of fastcrud? Please also show exactly the get_multi_joined code to get this result as well

@vnlebaoduy
Copy link
Author

@igorbenav i'm using fastcrud = "^0.11.0" . This is my code and schema:

@router.get("/plan", response_model=PaginatedListResponse[PlanJoinRead])
async def get_plan(
    request: Request, db: Annotated[AsyncSession, Depends(async_get_db)], page: int = 1, items_per_page: int = 10
) -> dict:
    plan_data = await crud_plan.get_multi_joined(db=db,
                                                 join_model=EncryptTemplate,
                                                 join_prefix="template_",
                                                 schema_to_select=PlanJoinRead,
                                                 join_schema_to_select=EncryptTemplateRead,
                                                 return_as_model=True,
                                                 page=page,
                                                 items_per_page=items_per_page,
                                                 )
    print(plan_data)
    response: dict[str, Any] = paginated_response(crud_data=plan_data, page=page, items_per_page=items_per_page)
    return response

This is schema

class AirforcePlanJoinRead(BaseModel):
    id: int
    title: str
    top_score: str
    template_id: int
    is_finished: bool
    description: Optional[str] = None
    created_at: datetime
    created_by: Optional[str] = None

@igorbenav
Copy link
Owner

Ok, I understand now. For the current implementation of fastcrud, it's not possible to get object in object response, it was like this by design. I think this might be an interesting feature though (specifying whether you want it flattened or not), and will work on it for the next version.

@igorbenav igorbenav added enhancement New feature or request FastCRUD Methods Related to FastCRUD methods labels Apr 15, 2024
@JakNowy
Copy link
Contributor

JakNowy commented Apr 18, 2024

What's great about the library is you should be able to override methods of either FastCRUD or the EndpointCreator to achieve what you need. It's much easier to adjust it to your needs than to design a complete architecture to handle all important cases in elagant way.

Nevertheless this feature seems really important to have. Do you have any roadmap on the mentioned "next version" @igorbenav ?

@igorbenav
Copy link
Owner

igorbenav commented Apr 19, 2024

I'll change stuff and it should be released over the weekend, there are a few other changes and I need to write the documentation for them as well.

@igorbenav igorbenav self-assigned this Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request FastCRUD Methods Related to FastCRUD methods
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants