Panther Models#
Panther models allow you to define your data schema and interact with the database using Python classes. They are built on top of pydantic.BaseModel
and extend its functionality with database query capabilities.
Creating a Model#
To create a Panther model, define a class that inherits from panther.db.Model
:
app/models.py | |
---|---|
1 2 3 4 5 6 |
|
Panther models inherit from both pydantic.BaseModel
and panther.db.Query
, giving you access to data validation and database queries.
Defining Attributes#
You can define model attributes (columns) using Python type hints. Each attribute type is handled as follows:
General Types#
- str, int, bool: Saved in the database as-is.
Example:
app/models.py | |
---|---|
1 2 3 4 5 6 |
|
List#
- list: Each item in the list is processed according to its type. The child type can be a primitive, a
pydantic.BaseModel
, or anotherModel
.
Example:
app/models.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Dictionary#
- dict: Each value in the dictionary is processed according to its type. Only plain
dict
is supported (not typed dicts likedict[str, int]
).
Example:
app/models.py | |
---|---|
1 2 3 4 |
|
Nested Models#
- pydantic.BaseModel: Treated like a dictionary, but with type information for each item. Each item is processed according to its type.
Example:
app/models.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
Foreign Keys#
- panther.db.Model: Treated as a foreign key relationship.
- The related model's value is stored in the database.
- Its
id
is saved in the main document (table). - This process is handled automatically, so you always have access to all attributes of the related model.
- Panther retrieves the corresponding value from the database and returns it as a fully populated model instance.
Example:
app/models.py | |
---|---|
1 2 3 4 5 6 7 8 |
|
Optional Attributes#
- You can make an attribute optional by using a union with
None
(e.g.,str | None
) and providing a default value (e.g.,= None
). - If you make an attribute optional, you must assign a default value.
Example:
app/models.py | |
---|---|
1 2 3 4 5 |
|