Skip to content

models

Models.

This module contains the data models for the tool inventory application. It includes models for creating, updating, and representing tools.

Tool

Bases: SQLModel

Tool model.

Source code in src/tool_inventory/models.py
class Tool(SQLModel, table=True):
    """Tool model."""

    id: UUID = Field(default_factory=uuid4, primary_key=True)
    name: str = Field(index=True, nullable=False, min_length=1)
    quantity: int = Field(default=0, ge=0)
    description: str = ""
    image: str = ""

ToolCreate pydantic-model

Bases: BaseModel

Tool creation model.

Show JSON schema:
{
  "description": "Tool creation model.",
  "properties": {
    "name": {
      "minLength": 1,
      "title": "Name",
      "type": "string"
    },
    "quantity": {
      "minimum": 0,
      "title": "Quantity",
      "type": "integer"
    },
    "description": {
      "default": "",
      "title": "Description",
      "type": "string"
    },
    "image": {
      "default": "",
      "title": "Image",
      "type": "string"
    }
  },
  "required": [
    "name",
    "quantity"
  ],
  "title": "ToolCreate",
  "type": "object"
}

Fields:

  • name (str)
  • quantity (int)
  • description (str)
  • image (str)
Source code in src/tool_inventory/models.py
class ToolCreate(BaseModel):
    """Tool creation model."""

    name: str = PydanticField(min_length=1)
    quantity: int = PydanticField(ge=0)
    description: str = ""
    image: str = ""

    def to_model(self) -> Tool:
        """Convert to a tool model.

        Returns:
            The tool model.
        """
        tool = Tool(
            name=self.name.strip(),
            quantity=self.quantity,
            description=self.description.strip(),
            image=self.image.strip(),
        )
        Tool.model_validate(tool)
        return tool

to_model

to_model() -> Tool

Convert to a tool model.

Returns:

Type Description
Tool

The tool model.

Source code in src/tool_inventory/models.py
def to_model(self) -> Tool:
    """Convert to a tool model.

    Returns:
        The tool model.
    """
    tool = Tool(
        name=self.name.strip(),
        quantity=self.quantity,
        description=self.description.strip(),
        image=self.image.strip(),
    )
    Tool.model_validate(tool)
    return tool

ToolPatch pydantic-model

Bases: BaseModel

Tool patch model.

Show JSON schema:
{
  "description": "Tool patch model.",
  "properties": {
    "name": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Name"
    },
    "quantity": {
      "anyOf": [
        {
          "type": "integer"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Quantity"
    },
    "description": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Description"
    },
    "image": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Image"
    }
  },
  "title": "ToolPatch",
  "type": "object"
}

Fields:

  • name (str | None)
  • quantity (int | None)
  • description (str | None)
  • image (str | None)
Source code in src/tool_inventory/models.py
class ToolPatch(BaseModel):
    """Tool patch model."""

    name: str | None = None
    quantity: int | None = None
    description: str | None = None
    image: str | None = None

    def patch(self, tool: Tool) -> Tool:
        """Patch a tool.

        Args:
            tool: The tool to patch.

        Returns:
            The patched tool.
        """
        if self.name is not None:
            tool.name = self.name
        if self.quantity is not None:
            tool.quantity = self.quantity
        if self.description is not None:
            tool.description = self.description
        if self.image is not None:
            tool.image = self.image
        return tool

patch

patch(tool: Tool) -> Tool

Patch a tool.

Parameters:

Name Type Description Default
tool Tool

The tool to patch.

required

Returns:

Type Description
Tool

The patched tool.

Source code in src/tool_inventory/models.py
def patch(self, tool: Tool) -> Tool:
    """Patch a tool.

    Args:
        tool: The tool to patch.

    Returns:
        The patched tool.
    """
    if self.name is not None:
        tool.name = self.name
    if self.quantity is not None:
        tool.quantity = self.quantity
    if self.description is not None:
        tool.description = self.description
    if self.image is not None:
        tool.image = self.image
    return tool