<jupyter_start><jupyter_text>Datamodels module examples<jupyter_code>from hero_db_utils.datamodels import datamodel
from hero_db_utils.datamodels.fields import ForeignKeyField, AutoSerialField
import pandas as pd
import dotenv
dotenv.load_dotenv(override=True);

@datamodel
class Store:
    address_id:ForeignKeyField("address","address_id")
    last_update:pd.to_datetime
    manager_staff_id:ForeignKeyField("staff", "staff_id") = None
    store_id:AutoSerialField() = None

@datamodel
class Address:
    address:str
    address2:str
    district:str
    city_id:ForeignKeyField("city", "city_id")
    postal_code:str
    phone:str
    last_update:pd.to_datetime
    address_id:AutoSerialField() = None

@datamodel
class City:
    city:str
    country_id:ForeignKeyField("country", "country_id")
    last_update:str
    city_id:AutoSerialField() = None

@datamodel
class Country:
    country:str
    last_update:pd.to_datetime
    country_id:AutoSerialField() = None

@datamodel
class Staff:
    first_name:str
    last_name:str
    address_id:ForeignKeyField("address","address_id")
    email:str
    store_id:ForeignKeyField("stores", "store_id")
    active:bool
    username:str
    password:str
    last_update:pd.to_datetime
    staff_id:AutoSerialField() = None

%env HERO_POSTGRES_DBNAME=dvd_rentals<jupyter_output>env: HERO_POSTGRES_DBNAME=dvd_rentals<jupyter_text>Select all from a model in the database:<jupyter_code>Store.objects.all()<jupyter_output><empty_output><jupyter_text>Filter from the table:<jupyter_code>from hero_db_utils.queries.postgres import QueryOp

Address.objects.filter(address_id=QueryOp.value_in([1,2]))<jupyter_output><empty_output><jupyter_text>Join on related table for column "address_id":<jupyter_code>Store.objects.fetch_related("address_id")<jupyter_output><empty_output><jupyter_text>Retrieve an object from the database:<jupyter_code>store = Store.objects.get(store_id=2)
store.data<jupyter_output><empty_output><jupyter_text>Update a model in the database:<jupyter_code>store.update(address_id=3)
store.address_id
Store.objects.all()<jupyter_output><empty_output><jupyter_text>Create a new staff member:<jupyter_code>Staff.objects.all()
new_staff = Staff(
    first_name="Harry",
    last_name="Potter",
    active = True,
    store_id=1,
    address_id=2,
    email="<EMAIL>",
    username="theboywholived",
    password="<PASSWORD>",
    last_update="now"
)
new_staff.data
# Insert staff in the database:
new_staff.insert()
# Check it exists in the database:
Staff.objects.all()<jupyter_output><empty_output>