Pydantic is a popular data validation library in Python. It allows you to define data models with attributes and validation rules. When you dump a Pydantic model, it will include all the fields in the model, including computed fields. However, sometimes you may not want to include computed fields in the dump. In this guide, we will show you how to exclude computed fields from Pydantic dumps.
What are Computed Fields?
In Pydantic, a computed field is a field that is not stored in the model's data, but is computed from other fields. Computed fields are defined using the @computed decorator. Here is an example:
from pydantic import BaseModel, computed
class Person(BaseModel):
first_name: str
last_name: str
@computed
def full_name(self):
return f"{self.first_name} {self.last_name}"
In this example, the Person model has two fields, first_name and last_name, and a computed field full_name. The full_name field is computed from the first_name and last_name fields. When you dump a Person instance, it will include the first_name and last_name fields, but not the full_name field.
Excluding Computed Fields
By default, Pydantic includes all fields, including computed fields, in the dump. However, you can exclude computed fields by setting the exclude_unset parameter to True in the dump method. Here is an example:
from pydantic import BaseModel, computed
from pydantic.json import dumps
class Person(BaseModel):
first_name: str
last_name: str
@computed
def full_name(self):
return f"{self.first_name} {self.last_name}"
person = Person(first_name="John", last_name="Doe")
# Dump the person instance, excluding computed fields
dumped_person = dumps(person, exclude_unset=True)
print(dumped_person)
In this example, the Person instance is dumped with the exclude_unset=True parameter. This will exclude the full_name field from the dump, and only include the first_name and last_name fields. The output will be:
{
"first_name": "John",
"last_name": "Doe"
}
Using a Custom Dumper
If you want more control over which fields to include or exclude in the dump, you can use a custom dumper. A custom dumper is a function that takes a Pydantic model instance and returns a dumped string. Here is an example:
from pydantic import BaseModel, computed
from pydantic.json import dumps
class Person(BaseModel):
first_name: str
last_name: str
@computed
def full_name(self):
return f"{self.first_name} {self.last_name}"
def custom_dumper(instance):
# Exclude the computed field from the dump
data = instance.dict(exclude={"full_name"})
# Dump the data to a string
dumped_data = dumps(data)
return dumped_data
person = Person(first_name="John", last_name="Doe")
# Dump the person instance using the custom dumper
dumped_person = custom_dumper(person)
print(dumped_person)
In this example, the custom_dumper function takes a Pydantic model instance and returns a dumped string. The dict method is used to create a dictionary from the instance, excluding the full_name field. The dumps function is then used to dump the dictionary to a string. The output will be the same as the previous example.
In this guide, we have shown you how to exclude computed fields from Pydantic dumps. By setting the exclude_unset parameter to True in the dump method, you can exclude all computed fields from the dump. If you want more control over which fields to include or exclude, you can use a custom dumper. With these techniques, you can customize the Pydantic dumps to fit your needs.
References
| Title | Link |
|---|---|
| Pydantic Documentation | https://pydantic-docs.helpmanual.io/ |
| Pydantic JSON Dumping | https://pydantic-docs.helpmanual.io/usage/exporting_models/#json-dumping |