Skip to content

Instantly share code, notes, and snippets.

@codemation
Created November 15, 2021 11:39
Show Gist options
  • Save codemation/5b3b0e870ad85e3f46f1f7e67e477c99 to your computer and use it in GitHub Desktop.
Save codemation/5b3b0e870ad85e3f46f1f7e67e477c99 to your computer and use it in GitHub Desktop.

Revisions

  1. codemation created this gist Nov 15, 2021.
    32 changes: 32 additions & 0 deletions pydbantic_model_7.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # models.py
    import uuid
    from datetime import datetime
    from typing import Optional
    from pydantic import BaseModel
    from pydbantic import DataBaseModel, PrimaryKey, Default

    def time_now_str():
    return datetime.now().isoformat()

    def stringify_uuid():
    return str(uuid.uuid4())

    class Coordinates(BaseModel):
    latitude: float = 52.299387
    longitude: float = 4.976949

    class Department(DataBaseModel):
    name: str = PrimaryKey()
    company: str
    location: Optional[Coordinates] = Coordinates()

    class Positions(DataBaseModel):
    name: str = PrimaryKey()
    position_department: Department = Department(name='HR', company='FOOBAR')

    class Employee(DataBaseModel):
    id: str = PrimaryKey(default=stringify_uuid)
    salary: float
    is_employed: bool
    date_employed: str = Default(default=time_now_str)
    position: Positions = Positions(name='Manager')