Item docs: mention Pydantic support (#6966)

* Add Pydantic support documentation

* Fix Sphinx references in Pydantic section for non-Sphinx docs

* Minor reformatting

---------

Co-authored-by: Adrian Chaves <adrian@zyte.com>
This commit is contained in:
Michael 2026-02-05 19:18:01 +01:00 committed by GitHub
parent 294ee051cc
commit 6a42bc6450
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 39 additions and 0 deletions

View File

@ -136,6 +136,45 @@ Example:
another_field = attr.ib()
.. _pydantic-items:
Pydantic models
---------------
`Pydantic <https://docs.pydantic.dev/>`_ models allow the defining of item
classes with field names, so that :ref:`item exporters <topics-exporters>` can
export all fields by default even if the first scraped object does not have
values for all of them.
Additionally, ``pydantic`` items also allow you to:
* define the type and default value of each defined field with run-time type
validation.
* define custom field metadata through `pydantic.Field
<https://docs.pydantic.dev/latest/concepts/fields/>`_, which can be used to
:ref:`customize serialization <topics-exporters-field-serialization>`.
* benefit from automatic data validation and conversion based on type
annotations.
In order to use this type, the `pydantic package <https://docs.pydantic.dev/>`_
needs to be installed.
Example:
.. code-block:: python
from pydantic import BaseModel, Field
class CustomItem(BaseModel):
one_field: str = Field(default="", description="First field")
another_field: int = Field(default=0, description="Second field")
.. note:: Unlike other item types, Pydantic models enforce field types at
run time and will raise validation errors for invalid data types.
Working with Item objects
=========================