Data Models
Data models represent the structure of the data that the API deals with. These models define the shape of the data entities exchanged between the client and the server.
For example, in this books management api project, a Book
model can be defined:
Create model/book.py:
from dataclasses import dataclass
from datetime import datetime
@dataclass
class Book:
id: int
title: str
author: str
published_at: str
description: str
isbn: str
total_pages: int
created_at: datetime
updated_at: datetime
Create model/__init__.py:
from .book import Book
Tip:
The__init__.py
file serves two main purposes in Python:
- Package Initialization: When Python imports a directory that contains an
__init__.py
file, it treats it as a package. The__init__.py
file is executed upon import of the package and can be used to perform package initialization tasks.- Symbol Exporting: It controls what symbols (functions, classes, variables) are available for import from the package. By importing modules or symbols into
__init__.py
, you can make them available for import directly from the package itself.