File size: 860 Bytes
047d92b
 
 
 
 
 
 
 
 
 
 
 
41168ea
047d92b
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from datetime import datetime, date
from sqlalchemy import BigInteger, Column, DateTime, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.inspection import inspect

Base = declarative_base()


class BaseModel_(Base):
    __abstract__ = True  # This model should not be instantiated directly
    __table_args__ = {"extend_existing": True}  # Allow table extension

    created = Column(DateTime, server_default=func.now(), nullable=False)

    def to_dict(self, keys_not_to_include: list[str] = []) -> dict:
        return {
            c.key: (
                getattr(self, c.key).isoformat()
                if isinstance(getattr(self, c.key), (datetime, date))
                else getattr(self, c.key)
            )
            for c in inspect(self).mapper.column_attrs
            if c.key not in keys_not_to_include
        }