Skip to content

Fields

beanie.odm.fields

Indexed

def Indexed(typ=None, index_type=ASCENDING, **kwargs: Any)

If typ is defined, returns a subclass of typ with an extra attribute _indexed as a tuple: - Index 0: index_type such as pymongo.ASCENDING - Index 1: kwargs passed to IndexModel When instantiated the type of the result will actually be typ.

When typ is not defined, returns an IndexedAnnotation instance, to be used as metadata in Annotated fields.

Example:

# Both fields would have the same behavior
class MyModel(BaseModel):
    field1: Indexed(str, unique=True)
    field2: Annotated[str, Indexed(unique=True)]

PydanticObjectId

class PydanticObjectId(ObjectId)

Object Id field. Compatible with Pydantic.

PydanticObjectId.__get_pydantic_json_schema__

@classmethod
def __get_pydantic_json_schema__(
        cls, schema: core_schema.CoreSchema,
        handler: GetJsonSchemaHandler) -> JsonSchemaValue

Results such schema:

{
    "components": {
        "schemas": {
            "Item": {
                "properties": {
                    "id": {
                        "$ref": "#/components/schemas/PydanticObjectId"
                    }
                },
                "type": "object",
                "title": "Item"
            },
            "PydanticObjectId": {
                "type": "string",
                "maxLength": 24,
                "minLength": 24,
                "pattern": "^[0-9a-f]{24}$",
                "example": "5eb7cf5a86d9755df3a6c593"
            }
        }
    }
}

FieldResolution

@dataclass(frozen=True)
class FieldResolution()

Immutable metadata attached to ExpressionField for query-time path resolution.

Tracks the nested model class (for alias resolution during field access) and whether the path crosses a Link/BackLink boundary (for DBRef translation at query time).

ExpressionField

class ExpressionField(str)

ExpressionField.__getitem__

def __getitem__(item)

Get sub field

Arguments:

  • item: name of the subfield

Returns:

ExpressionField

ExpressionField.__getattr__

def __getattr__(item)

Get sub field, resolving aliases from nested Pydantic models.

Alias resolution is performed through the model class carried in _field_resolution. The is_link flag is propagated from parent to child so that downstream query code can translate DBRef paths at runtime.

LinkInfo

class LinkInfo(BaseModel)

LinkInfo.document_class

Document class

class Link(Generic[T])

Link.fetch_list

@classmethod
async def fetch_list(cls,
                     links: list["Link[T] | DocType"],
                     fetch_links: bool = False)

Fetch list that contains links and documents

Arguments:

  • links:
  • fetch_links:

class BackLink(Generic[T])

Back reference to a document