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.

Skip optional fields

See original GitHub issue

Feature Request

We often drop pydantic objects into a NoSQL database where the individual object (document) structure is fairly sparse with quite a few defaults. To save space we often prune out the defaults as they can be regenerated on the fly fairly quickly. To this end, it would be useful to be able to drop results that have been defaulted when serializing automatically.

from pydantic import BaseModel
from typing import Optional

class User(BaseModel):
    id: int
    value: Optional[int]= None # Or some other decorator like Skip[int]?

u = User(id=5)
>>> print(u)
User id=5 value=None
>>> print(u.dict())
{'id': 5, 'value': None}

# Desired feature
>>> print(u.dict(skip_optional=True))
{'id': 5}

>>> u2 = User(id=5, value=5)
{'id': 5, 'value': 5}

Again, happy to add some code here if this is a feature you would consider.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
samuelcolvincommented, Feb 8, 2019

Yes

On Fri, 8 Feb 2019, 19:59 Daniel Smith <notifications@github.com wrote:

Ok, so we should add another slot for fields_set?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/samuelcolvin/pydantic/issues/378#issuecomment-461928019, or mute the thread https://github.com/notifications/unsubscribe-auth/AD2jGW3jfSPDGEZXlHdvDB943Zjt06AEks5vLdcQgaJpZM4aYr1_ .

0reactions
PaulRudincommented, Jul 3, 2021

Incidentally, you can achieve something similar by using a root validator with pre=True to add data for a derived field. So you don’t have a property, but you have the computed value created when the model is created. This works fine for immutable models (which is my use case).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Skip optional fields · Issue #378 · pydantic/pydantic - GitHub
when serialising the model, check whether each field's value matches its default, and ignore ones that do. keep a record of which values...
Read more >
How to skip Optional.empty fields during Jackson serialization?
I am hoping to get the serializer to skip the field if the Optional is empty, and serialize the contained string if it...
Read more >
Optional fields - Vest
Learn how to specify optional fields. ... to tell Vest to conditionally omit the results for this field by providing optional with a...
Read more >
Populate Signature field with value from API and Hide "skip ...
Only if the document contains optional field signers will get ''Skip optional fields'' and we will soon be enhancing this option based on...
Read more >
Handling optional fields — Chimney 0.6.2 documentation
By default, Chimney does the former (clears the value), but it also gives a simple way to always ignore None from patch with...
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