question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

annotate response model?

See original GitHub issue

Integrating 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:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dantownsendcommented, Aug 29, 2021

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.

create_pydantic_model(Band, nested_tables=[Band.manager])
1reaction
dantownsendcommented, Aug 28, 2021

There’s a couple of solutions I can think of, but I haven’t tried them out.

One option is a JSON field.

from pydantic import BaseModel, Json

class MyModel(BaseModel):
    id: int
    extra: Json  # Or could also be str

That way you can add extra return data into the JSON field.

The other approach is using optional fields in Pydantic:

import typing as t

from pydantic import BaseModel

class MyModel(BaseModel):
    id: int
    extra: t.Optional[str]

Not sure if either are what you’re after.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found