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.

How to use include/exclude with lists?

See original GitHub issue

Question

When using include/exclude with nested structures, how do I specify what to include/exclude when the child model is a list?

Where the child is a single element and I only want the id of that element, I know from the docs that I can do:

A.dict(include={"id": ..., "child": {"id": ...}})

Where the child is a list of a model, and I only want a single element of that list, and only the id of that element, I know from reading #640 that I can do:

A.dict(include={"id": ..., "children": {0: {"id": ...}}})

But what is the syntax (assuming there is a syntax) to specify that I want all children, but only the “id” field for all children?

Also, what would be the syntax (again, assuming it is possible) to specify that for the Parent, I want all top level attributes, but only the “id” of the nested attributes?

Thank you for your help and for the library.

Please complete:

  • OS: Linux
  • Python version import sys; print(sys.version): 3.7.4
  • Pydantic version import pydantic; print(pydantic.VERSION): 1.0

Please read the docs and search through issues to confirm your question hasn’t already been answered.

Where possible please include a self contained code snippet describing your question:

from __future__ import annotations
from typing import List, Optional

from pydantic import BaseModel, VERSION as pydantic_version
from dataclasses import dataclass


class Parent(BaseModel):
    id: int
    child: Child
    children: List[Child] = []

    class Config:
        orm_mode = True

class Child(BaseModel):
    id: int
    age: int

    class Config:
        orm_mode = True

Parent.update_forward_refs()

@dataclass
class OrmChild:
    id: int
    age: int

@dataclass
class OrmParent:
    id: int
    children: List[OrmChild]
    child: OrmChild

c1 = OrmChild(1, 12)
c2 = OrmChild(2, 7)
c3 = OrmChild(3, 2)
a = OrmParent(id=1, children=[c1, c2, c3], child=c1)

A = Parent.from_orm(a)

print(A.dict(
# WHAT GOES HERE?!
)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
parchd-1commented, Nov 3, 2019

I can only assume what other people would commonly require but I think

But what is the syntax (assuming there is a syntax) to specify that I want all children, but only the “id” field for all children?

This is something I expect people to want quite commonly - more commonly than wanting to have just the _n_th element of a list, which was one of the test cases you discussed when developing the syntax.

Also, what would be the syntax (again, assuming it is possible) to specify that for the Parent, I want all top level attributes, but only the “id” of the nested attributes?

The reason I want this is for an API, with nested models, some of which are quite large, I only want to send to the front end the top level attributes and a subset of children elements to be fetched if required. I think this is also quite commonly needed in APIs, but it is not my area of expertise.

4reactions
wendersonjcommented, Jan 21, 2022

when trying to exclude a bool , i got the message:

  File "pydantic\main.py", line 779, in pydantic.main.BaseModel._get_value
  File "pydantic\utils.py", line 479, in pydantic.utils.ValueItems.__init__
TypeError: Unexpected type of exclude value <class 'bool'> ```

answering my question: the error occurs in pydantic version 1.8.2. When updated to version 1.9.0 everything went fine ! cheers

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating an include-exclude list - IBM
You can create an include-exclude list to exclude a specific file or groups of files from backup services, and to assign specific management...
Read more >
How to use include/exclude with lists? · Issue #959 - GitHub
Question When using include/exclude with nested structures, how do I specify what to include/exclude when the child model is a list?
Read more >
Include Exclude Lists for Schemas and Tables - Tokern
Control which schemas and tables are scanned by PIICatcher using include/exclude lists.
Read more >
Defining Include/Exclude Lists - BMC Documentation
The Include/Exclude List screen displays lists created by all users on the system. You can use or copy any existing list, but you...
Read more >
TSSA: Using include/exclude lists within a Patch Analysis Job
This video demonstrates how to use include/exclude lists within a Patch Analysis Job.Additional information about using include/exclude ...
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