Pydantic will exclude the class variables which begin with an underscore. so if it fits your use case, you can rename your attribues.

class User(UserBase):
    _user_id=str
    some_other_field=str
    ....
Answer from N.Moudgil on Stack Overflow
🌐
Pydantic
docs.pydantic.dev › latest › concepts › serialization
Serialization - Pydantic Validation
For serialization, field inclusion and exclusion can be configured in two ways: at the field level, using the exclude and exclude_if parameters on the Field() function.
Discussions

Pydantic v2 exclude parameter in Field and Annotated
I think that it's because you annotated it as Optional[Annotated[User, Field(exclude=True)]]. That basically means either None or annotated User. Try this Annotated[Optional[User], Field(exclude=True)] Your typing is kind of weird, you say you want it to be either None or excluded. And I think pydantic cannot resolve it because it's not applied to all the possibilities in the field. If I should guess, pydantic doesn't even check for Annotated when it's not the root type as it doesn't make much sense. More on reddit.com
🌐 r/learnpython
5
1
January 1, 2024
Dynamically include/exclude fields from dumps
However, the field_serializer does not allow to exclude the field from serialization. At best the value can be set to None but, it won't be respected by exclude_none: >>> from typing import Optional >>> from pydantic import field_serializer, BaseModel >>> class Foo(BaseModel): ... More on github.com
🌐 github.com
1
May 30, 2024
Struggling with Pydantic 'excludes'
Forgive me if this doesn't answer the question at hand, I'm doing my best with a short amount of time. It looks like the problem here is dealing with lists. To handle a list, you'll have to use a validator, as field will not peek into a list to check the elements inside. Here's the relevant docs: https://docs.pydantic.dev/usage/validators/ The tl;dr here is that you'll set up your model and then set up a validator by having the validator decorator and setting up the validation function beneath it. You'll also want to set for_each to true in the decorator so it knows to check each item in the list. So, as a big hammer approach, something like @validator('the_name_of_the_field_you_want_to_check', for_each=True) def scrub_children(cls, v): v.parent_node = None return v Might fix your problem. More on reddit.com
🌐 r/FastAPI
4
5
March 2, 2023
`include` and `exclude` are not passed to field serializer contexts
When using model_dump with include ... in the field serializer SerializationInfo object, like it is for model serializers. However, it seems info.include and info.exclude are always empty. Is it intentional? If so I could not find documentation about this behavior. >>> from pydantic import BaseModel, ... More on github.com
🌐 github.com
5
June 1, 2024
🌐
GitHub
github.com › pydantic › pydantic › discussions › 5461
`exclude_none` for specific fields only · pydantic/pydantic · Discussion #5461
Thanks for this, it worked for me though I had to tweak it to take into account Field aliases and to run PlainSerializer(s): from dataclasses import dataclass from pydantic import BaseModel, PlainSerializer, model_serializer @dataclass class OmitIfNone: pass class AppResponseModel(BaseModel): @model_serializer def _serialize(self): skip_if_none = set() serialize_aliases = dict() # Gather fields that should omit if None for name, field_info in self.model_fields.items(): if any( isinstance(metadata, OmitIfNone) for metadata in field_info.metadata ): skip_if_none.add(name) elif field_info.seriali
Author   pydantic
🌐
GitHub
github.com › pydantic › pydantic › discussions › 7114
Exclude a computed field · pydantic/pydantic · Discussion #7114
August 14, 2023 - What is the point of excluding a computed field from the serialization output? If you don't want it to be included, you can use a simple @property. Or maybe you want to exclude it when using your_model_instance.model_dump(exclude="your_comp...
Author   pydantic
🌐
Pydantic
docs.pydantic.dev › 2.11 › concepts › serialization
Serialization - Pydantic
The model_dump and model_dump_json methods support include and exclude parameters which can either be sets or dictionaries. This allows nested selection of which fields to export: from pydantic import BaseModel, SecretStr class User(BaseModel): id: int username: str password: SecretStr class Transaction(BaseModel): id: str user: User value: int t = Transaction( id='1234567890', user=User(id=42, username='JohnDoe', password='hashedpassword'), value=9876543210, ) # using a set: print(t.model_dump(exclude={'user', 'value'})) #> {'id': '1234567890'} # using a dict: print(t.model_dump(exclude={'user': {'username', 'password'}, 'value': True})) #> {'id': '1234567890', 'user': {'id': 42}} print(t.model_dump(include={'id': True, 'user': {'id'}})) #> {'id': '1234567890', 'user': {'id': 42}}
🌐
Reddit
reddit.com › r/learnpython › pydantic v2 exclude parameter in field and annotated
r/learnpython on Reddit: Pydantic v2 exclude parameter in Field and Annotated
January 1, 2024 -

I am playing around with Pydantic v2.5 and trying to see how the exclude works when set as a Field option. Let's imagine that I have a User BaseModel class and a Permissions BaseModel class. Both are used in the Config class.

We do not want to print the all User info, hence why I added the exclude in the Permissions class when the user is defined.

from typing import Annotated, Optional
from pydantic import BaseModel, Field


class User(BaseModel):
    name: str
    surname: str
    card_id: str


class Permissions(BaseModel):
    user: Annotated[User, Field(exclude=True)]
    config_rule: str


class Config:
    def __init__(self):
        self.user = User(**dict(name="Name", surname="Surname", card_id="12343545"))
        self.config_data = Permissions(user=self.user, config_rule="admin")

c = Config()
print(c.config_data.model_dump_json(indent=2))

In this case, the exclude set in the Permissions class seems to be working fine, in fact the print output is:

{
  "config_rule": "admin"
}

However, if I change the Permissions class with:

class Permissions(BaseModel):
    user: Optional[Annotated[User, Field(exclude=True)]]
    config_rule: str

The output is:

{
  "user": {
    "name": "Name",
    "surname": "Surname",
    "card_id": "12343545"
  },
  "config_rule": "admin"
}

And I am not sure I understand why. Any thoughts? Thanks.

🌐
Pydantic
docs.pydantic.dev › latest › concepts › fields
Fields - Pydantic Validation
Note that the by_alias keyword argument defaults to False, and must be specified explicitly to dump models using the field (serialization) aliases. You can also use ConfigDict.serialize_by_alias to configure this behavior at the model level. When by_alias=True, the alias 'username' used during serialization. If you want to use an alias only for validation, you can use the validation_alias parameter: from pydantic import BaseModel, Field class User(BaseModel): name: str = Field(validation_alias='username') user = User(username='johndoe') # (1)!
🌐
Sentry
sentry.io › sentry answers › fastapi › exclude one or more fields in pydantic model from appearing in fastapi responses
Exclude one or more fields in Pydantic model from appearing in FastAPI responses | Sentry
This special typing form was proposed in PEP 593 and is used to add specific metadata to type declarations. This metadata is being used by libraries such as Pydantic to enable special behavior. In this case, we’ll use it to exclude the address and created_at fields from our API’s responses:
Find elsewhere
🌐
GitHub
github.com › pydantic › pydantic › issues › 9528
Dynamically include/exclude fields from dumps · Issue #9528 · pydantic/pydantic
May 30, 2024 - However, the field_serializer does not allow to exclude the field from serialization. At best the value can be set to None but, it won't be respected by exclude_none: >>> from typing import Optional >>> from pydantic import field_serializer, ...
Published   May 30, 2024
Author   azmeuk
🌐
GitHub
github.com › pydantic › pydantic › discussions › 11724
How about using a special value in the field serializer to exclude a field? · pydantic/pydantic · Discussion #11724
class Model(BaseModel): foo: int ... flexible addition to the existing exclusion mechanisms in Pydantic: Field(exclude=True), model_dump(exclude={})....
Author   pydantic
🌐
Reddit
reddit.com › r/fastapi › struggling with pydantic 'excludes'
r/FastAPI on Reddit: Struggling with Pydantic 'excludes'
March 2, 2023 -

I'm building an API that deals with a bunch of related data structures. And I'm currently struggling with some of the intricacies of Pydantic. Here's my problem. I hope someone out there is able to help me out here! :)

Consider a Pydantic schema like this:

class Node(BaseModel):

    name: str

    uuid: UUID

    parent_node_uuid: UUID | None

With that it is possible to represent a hierarchical, tree-like data set. Individual node objects are related to each other through the parent_node_uuid property. Each parent node can have multiple children. But each child can only ever have a single parent. (in other words there is a self-referential one-to-many relationship)

Now, when outputting this data set through the api endpoint, I could simply use the above schema as my response_model. But instead I want to make the data more verbose and instead nest the model, so that the output of one node includes all the information about its parent and child nodes. The naive approach looks like this:

class Node(BaseModel):

    name: str

    uuid: UUID

    parent_node: "Node" | None = None

    child_nodes: List["Node"] = []

Unfortunately, this does not work as intended. When I try to pass sqlalchemy objects (which do have the required relationships set up) to this Pydantic schema, I'm running into an infinite recursion and python crashes. The reason is that the parent_node includes the main node object inits child_nodes property. Similarly, each child_node of our main node will have the main node set as their parent_node. - it's easy to see how Pydantic gets stuck in an infinite loop here.

There is a solution to one part of this problem:

class Node(BaseModel):

    name: str

    uuid: UUID

    parent_node: "Node" | None = Field(None, exclude={"child_nodes"}) 

    child_nodes: List["Node"] = []

Using the exclude option, we're able to remove the child_nodes from the parent. - That's one half of the issue resolved. The above model now works in cases where our main node doesn't have any children, but it has a parent. (more on this in the docs here)

Unfortunately though, this solution does not work with lists. I've tried the following without success:

class Node(BaseModel):

    name: str

    uuid: UUID

    parent_node: "Node" | None = Field(None, exclude={"child_nodes"}) 

    child_nodes: List["Node"] = Field([], exclude={"parent_node"})

*Does anyone know how I can get the exclude parameter to work when dealing with a List of models? *

🌐
GitHub
github.com › pydantic › pydantic › issues › 9538
`include` and `exclude` are not passed to field serializer contexts · Issue #9538 · pydantic/pydantic
June 1, 2024 - @field_serializer("bar") ... def ser_bar(value: Any, info:SerializationInfo): ... if not info.include: ... raise ValueError("no include") ... return value >>> Foo(bar="baz").model_dump(include={"bar"}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/eloi/.virtualenvs/pydantic-scim2-dlnh/lib/python3.12/site-packages/pydantic/main.py", line 347, in model_dump return self.__pydantic_serializer__.to_python( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pydantic_core._pydantic_core.PydanticSerializationError: Error calling function `ser_bar`: ValueError: no include
Author   azmeuk
🌐
GitHub
github.com › samuelcolvin › pydantic › issues › 1862
Is it possible to exclude fields from the model itself when exporting them? (instead of dict(exclude=..)) · Issue #1862 · pydantic/pydantic
August 23, 2020 - (instead of dict(exclude=..))#1862 ... class MyCls(BaseModel): permissions: Permissions some_id: str = Field('') some_flag: bool = Field(False)
Author   arielb135
🌐
Pydantic
docs.pydantic.dev › 1.10 › usage › exporting_models
Exporting models - Pydantic
The dict, json, and copy methods support include and exclude arguments which can either be sets or dictionaries. This allows nested selection of which fields to export: ... from pydantic import BaseModel, SecretStr class User(BaseModel): id: int username: str password: SecretStr class Transaction(BaseModel): id: str user: User value: int t = Transaction( id='1234567890', user=User( id=42, username='JohnDoe', password='hashedpassword' ), value=9876543210, ) # using a set: print(t.dict(exclude={'user', 'value'})) #> {'id': '1234567890'} # using a dict: print(t.dict(exclude={'user': {'username', 'password'}, 'value': True})) #> {'id': '1234567890', 'user': {'id': 42}} print(t.dict(include={'id': True, 'user': {'id'}})) #> {'id': '1234567890', 'user': {'id': 42}}
🌐
Pydantic
docs.pydantic.dev › latest › api › config
Configuration - Pydantic Validation
If you have an Optional[Enum] value that you set a default for, you need to use validate_default=True for said Field to ensure that the use_enum_values flag takes effect on the default, as extracting an enum's value occurs during validation, ...
🌐
GitHub
github.com › pydantic › pydantic › issues › 8220
Manually setting `exclude` doesn't work · Issue #8220 · pydantic/pydantic
November 24, 2023 - I know I can use exclude_unset, exclude_none and so on in the model_dump but for my needs I want to set specific fields to not be serialized · pydantic version: 2.5.1 pydantic-core version: 2.14.3 pydantic-core build: profile=release pgo=true install path: venv/lib/python3.11/site-packages/pydantic python version: 3.11.1 (main, Jan 17 2023, 22:23:12) [Clang 13.0.0 (clang-1300.0.29.3)] platform: macOS-13.2.1-arm64-arm-64bit related packages: typing_extensions-4.8.0 mypy-1.1.1
Author   artuntun
Top answer
1 of 2
4

A possible solution that works for pydantic 2.* is to use the @model_serializer decorator. The decorator allows to define a custom serialization logic for a model.

# or `from typing import Annotated` for Python 3.9+
from typing_extensions import Annotated

from typing import Optional
from pydantic import BaseModel
from pydantic.functional_serializers import model_serializer

class OmitIfNone:
    pass

class NoSerializeNoneModel(BaseModel):
    @model_serializer
    def _serialize(self):
        omit_if_none_fields = {
            k
            for k, v in self.model_fields.items()
            if any(isinstance(m, OmitIfNone) for m in v.metadata)
        }

        return {k: v for k, v in self if k not in omit_if_none_fields or v is not None}

class UserResponseModel(NoSerializeNoneModel):
    id: int
    msg: Annotated[Optional[str], OmitIfNone()] = None

You can see that the msg field is not serialized when None:

a = UserResponseModel(id=1)
print(a.json())
# {"id":1}

b = UserResponseModel(id=1, msg="Hello")
print(b.json())
# {"id":1,"msg":"Hello"}

Disclaimer: this answer was inspired by this comment on a related Github issue.

2 of 2
0

you can't do such thing with pydantic and even with more powerfull lib like attrs. The why may be because it is not a good way of returning json object, it is realy confusing for you, the api client and your test suite.

you may get some inspiration from elegant-way-to-remove-fields-from-nested-dictionaries.

you would be able to achieve something (not recommanded at all) by parsing your object jsoned and remove fiels folowing a logic.

exemple of key/value manipulation in nested dict:

import re

def dict_key_convertor(dictionary):
    """
    Convert a dictionary from CamelCase to snake_case
    :param dictionary:  the dictionary given
    :return: return a dict
    """
    if not isinstance(dictionary, (dict, list)):
        return dictionary

    if isinstance(dictionary, list):
        return [dict_key_convertor(elem) for elem in dictionary]

    return {to_snake(key): dict_key_convertor(data) for key, data in dictionary.items()}


def to_snake(word) -> str:
    """
    Convert all word from camel to snake case
    :param word: the word given to be change from camelCase to snake_case
    :return: return word variable in snake_case
    """
    return re.sub(r'([A-Z]{2,}(?=[a-z]))', '\\1_', re.sub(r'([a-z])([A-Z]+)', '\\1_\\2', word)).lower()

with a bit of work you may achive something with this:

from typing import List


def dict_key_cleaner(dictionary):
    if not isinstance(dictionary, (dict, list)):
        return dictionary

    if isinstance(dictionary, list):
        return [dict_key_cleaner(elem) for elem in dictionary]
    # change this return to work with dict
    return {poper(key, dictionary): dict_key_cleaner(data) for key, data in dictionary.items()}


def poper(key, dictionary):
    special_keys: List[str] = ["field_name","field_name1","field_name2"]
    # do some stuff here
    for spe_key in special_keys:
        if key == spe_key and key.key_value is None:
            dictionary.pop(key)
            # add return of modified dict

🌐
Readthedocs
piccolo-orm.readthedocs.io › en › latest › piccolo › serialization › index.html
Serialization — Piccolo 1.28.0 documentation - Read the Docs
include_columns – A tuple of Column instances that should be included in the Pydantic model. Only specify include_columns or exclude_columns. include_default_columns – Whether to include columns like id in the serialiser. You will typically include these columns in GET requests, but don’t require them in POST requests. include_readable – Whether to include ‘readable’ columns, which give a string representation of a foreign key. all_optional – If True, all fields are optional.
🌐
Medium
skaaptjop.medium.com › how-i-use-pydantic-unrequired-fields-so-that-the-schema-works-0010d8758072
How I use Pydantic unrequired fields without defaults | by Will van der Leij | Medium
May 22, 2024 - I would need to exclude_unset to make sure I don’t have a bunch of useless data in my serialized dict. This is not too bad because that is what I designed my schema to do… don’t give me data and I won’t have any data. But then instead of having biolerplate looking for if field is None: I’m just going to have to have boilerplate that asks if data.get(‘address’) etc.