Pydantic Foreign Key feature metaclass.
Project description
Pydantic Foreign Key
Create a new metaclass for Pydantic BaseData that adds the concept of Foreign Key to a Pydantic Model, this means that fields from different models can be linked together and can eventually share it's instance values, like a Foreign Key in a database.
Example of Use
from pydantic import BaseModel
from pydantic_fk import LinkModelMetaclass
from .microservice import MicroserviceFoo, MicroserviceBar
import mariadb
class DatabaseSettings(BaseModel, metaclass=LinkModelMetaclass):
host: str
port: int
username: str
password: str
database: str
class FooSettings(BaseModel, metaclass=LinkModelMetaclass):
_links = {"db": DatabaseSettings}
title: str
description: str
class BarSettings(BaseModel, metaclass=LinkModelMetaclass):
_links = {"db": DatabaseSettings}
id: int
key: str
class AppSettings(BaseModel, metaclass=LinkModelMetaclass):
default_database_settings: DatabaseSettings
foo: FooSettings
bar: BarSettings
if __name__ == "__main__":
settings = AppSettings.model_validate({
"default_database_settings": {
"host": "database.localdomain",
"port": 3306,
"username": "dbuser",
"password": "dbpass",
"database": "app"},
"foo": {
"title": "My MicroService",
"description": "This is an example of MicroService connecting to database"},
"bar": {
"id": 1,
"key": "My.S3cre+.Key",
"db_database": "bardb"}})
foo_db_settings = {
k[3:]: v
for k, v settings.foo.model_dump().items()
if k.startswith("db_")}
bar_db_settings = {
k[3:]: v
for k, v settings.bar.model_dump().items()
if k.startswith("db_")}
foo = MicroserviceFoo(database=mariadb.connect(**foo_db_settings))
bar = MicroserviceBar(database=mariadb.connect(**bar_db_settings))
foo.start()
bar.start()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_fk-1.0.0.tar.gz.
File metadata
- Download URL: pydantic_fk-1.0.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7c5e628d1468be51f7a890352eede50e000cdcfb49fa3054616c521da3ded23
|
|
| MD5 |
8abb0aa24ebf35be5a77571850f73244
|
|
| BLAKE2b-256 |
64a9890b3e6b3e3248a4aa20832a70e0b7451e9840d9051583047459990077aa
|
File details
Details for the file pydantic_fk-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_fk-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5706454b6f4527413ea3abbaccde2943e8aa341350a0ce516ca7adce94ed01de
|
|
| MD5 |
96a5dc94da0a5c6ef4c71f7eec18bfea
|
|
| BLAKE2b-256 |
a5e89e42b47afa1b70a8f998f1a3bea7263971dbccd07f8fb430769263f9dd14
|