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

How to Deserialize an ActiveModel ? and a request for Serialization examples #457

Closed
VaZark opened this issue Jan 14, 2022 · 7 comments
Closed

Comments

@VaZark
Copy link

VaZark commented Jan 14, 2022

I'm trying to implement a basic CRUD app and I'm a bit lost. I'm trying to create a REST API with Axum. I've been trying to use the Json(new_active_model). Is this not supported by Serde yet ?

 let new_task = task::ActiveModel {
        title: Set(payload.title),
        text: Set(payload.text),
        ..Default::default()
    }
    .save(conn)
    .await
    .expect("could not insert task");

    // this will be converted into a JSON response
    // with a status code of `201 Created`
    (StatusCode::CREATED, Json(new_task))

Regardless of the problem that I'm facing, It'd be nice if the examples and/or docs were updated showing how to serialize and deserialize entities/models, as well as, examples for REST APIs

@billy1624
Copy link
Member

billy1624 commented Jan 15, 2022

Hey @VaZark, you can check web API demo in the /examples folder. Example for Actix, Rocket and Poem (#446) are included

@VaZark
Copy link
Author

VaZark commented Jan 15, 2022

hI @billy1624, I played with the examples but it seems like the underlying issue is the trait Serialize is not implemented for task::ActiveModel. (I generated the model with the sea-orm-cli generate entity)

So can the ActiveModel add support for Serialize? Or can I convert an ActiveModel to just a Model and just use derive Serialize on it?

I'm pretty new to rust so you can feel free to point out if I'm missing /misunderstanding something here

@billy1624
Copy link
Member

If you want to get back the inserted / updated model in Model instead of ActiveModel. You can use insert / update method in sea-orm 0.5.x

@VaZark
Copy link
Author

VaZark commented Jan 16, 2022

Thanks. Insert was what I needed to get it working. Is it not possible to make the ActiveModel support Serialize?

@billy1624
Copy link
Member

Thanks. Insert was what I needed to get it working. Is it not possible to make the ActiveModel support Serialize?

Is there any reason you need to Serialize ActiveModel? I guess what you need is just getting POST data (Model without primary key attribute if it's insert) from end user and responding the saved Model

@VaZark
Copy link
Author

VaZark commented Jan 25, 2022

Yup. That's what I ended up doing. I was trying to come up with ways to not write new types by hand for each table.

@VaZark VaZark closed this as completed Jan 25, 2022
@billy1624
Copy link
Member

Nice! Feel free to ping us if you got any other questions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants