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.

Nested model not included in Pydantic model

See original GitHub issue

Describe the bug I don’t know if it comes from FastAPI, Pydantic or Ormar. When retrieving a Pydantic model from an Ormar model, the response example shown in the Swagger interface does not include a nested model. Same with the actual responses: they do not contain that nested model.

My models:

# meta classes removed for conciseness

class Library(ormar.Model):
    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)


class Package(ormar.Model):
    id: int = ormar.Integer(primary_key=True)
    library: Library = ormar.ForeignKey(Library, related_name="packages")  # <-- of interest
    version: str = ormar.String(max_length=100)


class Ticket(ormar.Model):
    id: int = ormar.Integer(primary_key=True)
    number: int = ormar.Integer()
    status: str = ormar.String(max_length=100)


class TicketPackage(ormar.Model):
    id: int = ormar.Integer(primary_key=True)
    status: str = ormar.String(max_length=100)
    ticket: Ticket = ormar.ForeignKey(Ticket, related_name="packages")
    package: Package = ormar.ForeignKey(Package, related_name="tickets")  # <-- of interest

My route:

router = APIRouter(prefix="/tickets")
TicketPackageOut = TicketPackage.get_pydantic(exclude={"ticket"})


@router.get(
    "/{ticket_id}/packages",
    response_model=List[TicketPackageOut],
)
async def get_ticket_packages(ticket_id: int) -> List[TicketPackage]:
    return await TicketPackage.objects.select_related("package__library").filter(ticket__id=ticket_id).all()
    # Note how I "select the related package library".
    # If I print the return value of that query,
    # I can see that the library objects are here
    # with all their values (name) populated, as expected.

Expected behavior

The example and actual responses in Swagger should look like:

[
  {
    "id": 0,
    "status": "string",
    "package": {
      "version": "string",
      "id": 0,
      "library": {
        "id": 0,
        "name": "string"
      }
    }
  }
]

Actual behavior

The responses look like:

[
  {
    "id": 0,
    "status": "string",
    "package": {
      "version": "string",
      "id": 0
    }
  }
]

The package library is missing!

Screenshots

Example response Actual response
image image

You’ll notice extra fields that I removed from the code above for conciseness.

Versions:

  • Database backend used: sqlite
  • Python version: 3.8.11
  • ormar version: 0.10.14
  • pydantic version: 1.8.2
  • if applicable fastapi version: 0.65.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
pawamoycommented, Jul 21, 2021

Well this diff seems to fix the issue!

                 relation_map=cls._skip_ellipsis(
-                    relation_map, field, default_return=dict()
+                    relation_map, name, default_return=dict()
                 ),
0reactions
collerekcommented, Jul 21, 2021

Fixed by #280

Read more comments on GitHub >

github_iconTop Results From Across the Web

Define a Pydantic (nested) model - python - Stack Overflow
I would like to create a Pydantic model for managing this data structure (I mean to formally define these objects).
Read more >
Body - Nested Models - FastAPI
Body - Nested Models¶. With FastAPI, you can define, validate, document, and use arbitrarily deeply nested models (thanks to Pydantic).
Read more >
Models - pydantic
User here is a model with two fields id which is an integer and is required, and name which is a string and...
Read more >
Serialization — Piccolo 0.101.0 documentation - Read the Docs
Another great feature is nested=True . For each ForeignKey in the Piccolo Table , the Pydantic model will contain a sub model for...
Read more >
Settings management - pydantic
If you create a model that inherits from BaseSettings , the model initialiser will attempt to determine the values of any fields not...
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