Skip to main content

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()

Release files for pydantic-fk 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-fk 1.0.0
File Size Uploaded
pydantic_fk-1.0.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-fk 1.0.0
File Interpreter ABI Platform
pydantic_fk-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.5 kB

Release files / pydantic_fk-1.0.0.tar.gz

Download URL pydantic_fk-1.0.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b7c5e628d1468be51f7a890352eede50e000cdcfb49fa3054616c521da3ded23
BLAKE2b-256 checksum
How to use checksums
64a9890b3e6b3e3248a4aa20832a70e0b7451e9840d9051583047459990077aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / pydantic_fk-1.0.0-py3-none-any.whl

Download URL pydantic_fk-1.0.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5706454b6f4527413ea3abbaccde2943e8aa341350a0ce516ca7adce94ed01de
BLAKE2b-256 checksum
How to use checksums
a5e89e42b47afa1b70a8f998f1a3bea7263971dbccd07f8fb430769263f9dd14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page