annotate response model?
See original GitHub issueIntegrating Piccolo ORM with FastAPI! It’s a joy. However, I’m stumbling over a situation where I want to annotate a model response object with an additional key/value pair. (The response models have been automatically generated through create_pydantic_model, so I don’t see a way of adding fields except by subclassing – but the kind of annotations I want are more transient than this). The API is returning models from the database via .select(), and it’s here that I would want to add these transient annotations.
Imagine, as it were:
class PersonModelOut: # but created by create_pydantic_model, based on Piccolo ORM table
name: str
age: int
…in the API, where the response model is PersonModelOut:
return Person.select(Person.name, Person.age, Person.annotation( { "disposition": "cranky" } ).where(Person.age > 40).run()
Or is this totally off the wall?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Spring's RequestBody and ResponseBody Annotations
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the ...
Read more >Spring @ResponseBody Annotation with Example
When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.
Read more >How does the Spring @ResponseBody annotation work?
First of all, the annotation doesn't annotate List . It annotates the method, just as RequestMapping does. Your code is equivalent to
Read more >Response Model - FastAPI
FastAPI framework, high performance, easy to learn, fast to code, ready for production.
Read more >Spring Boot @ResponseBody - binding controller ... - ZetCode
@ResponseBody is a Spring annotation which binds a method return value to the web response body. It is not interpreted as a view...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That’s right. I think this would be a valuable feature. I’ll try and add it.
It would be worth upgrading
create_pydantic_model
in a similar way, so it can also generate nested models.There’s a couple of solutions I can think of, but I haven’t tried them out.
One option is a JSON field.
That way you can add extra return data into the JSON field.
The other approach is using optional fields in Pydantic:
Not sure if either are what you’re after.