Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pydantic model and ORM model

from typing import List
from sqlalchemy import Column, Integer, String
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base
from pydantic import BaseModel, constr

Base = declarative_base()


class CompanyOrm(Base):
    __tablename__ = 'companies'
    id = Column(Integer, primary_key=True, nullable=False)
    public_key = Column(String(20), index=True, nullable=False, unique=True)
    name = Column(String(63), unique=True)
    domains = Column(ARRAY(String(255)))


class CompanyModel(BaseModel):
    id: int
    public_key: constr(max_length=20)
    name: constr(max_length=63)
    domains: List[constr(max_length=255)]

    class Config:
        orm_mode = True


co_orm = CompanyOrm(
    id=123,
    public_key='foobar',
    name='Testing',
    domains=['example.com', 'foobar.com'],
)
print(co_orm)
#> <models_orm_mode.CompanyOrm object at 0x7f48594a0e20>
co_model = CompanyModel.from_orm(co_orm)
print(co_model)
#> id=123 public_key='foobar' name='Testing' domains=['example.com',
#> 'foobar.com']
Comment

PREVIOUS NEXT
Code Example
Python :: accessing 2d list in python 
Python :: python tkinter gui does not update until function completes 
Python :: Display complete information about the DataFrame 
Python :: select data frame with categorical datatype in pandas 
Python :: how to remove hidden white spaces n columns 
Python :: Python Tkinter MenuButton Widget Syntax 
Python :: all python 
Python :: percent change pandas using log 
Python :: design patterns python - restrict what methods of the wrapped class to expose 
Python :: python Prefix Sum of Matrix (Or 2D Array) 
Python :: page views count django 
Python :: palindrome program in python 
Python :: python yield async await 
Python :: python show difference between two strings and colorize it 
Python :: operator overloading in python 
Python :: Sampling data in different ways 
Python :: reverse color matplotlib 
Python :: needle in haystack 
Python :: how to append the items in list 
Python :: python pattern glob extension searching 
Python :: jupyter notebook print formatted text 
Python :: india states django choices 
Python :: python when to use pandas series, numpy ndarrays or simply python dictionaries 
Python :: what is require_self 
Python :: c# script for download music from telegram channel 
Python :: matlab index last element 
Python :: how to display text on boxplot in python 
Python :: how to check if an array is empty in python 
Python :: Python Syntax of for Loop 
Python :: Python Getting back to Decorators 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =