version with pydantic inheritance passing all the tests
This commit is contained in:
80
ormar/fields/base.py
Normal file
80
ormar/fields/base.py
Normal file
@ -0,0 +1,80 @@
|
||||
from typing import Any, Dict, List, Optional, TYPE_CHECKING
|
||||
|
||||
import pydantic
|
||||
import sqlalchemy
|
||||
from pydantic import Field
|
||||
|
||||
from ormar import ModelDefinitionError # noqa I101
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
from ormar.models import Model
|
||||
|
||||
|
||||
def prepare_validator(type_):
|
||||
def validate_model_field(value):
|
||||
return isinstance(value, type_)
|
||||
|
||||
return validate_model_field
|
||||
|
||||
|
||||
class BaseField:
|
||||
__type__ = None
|
||||
|
||||
column_type: sqlalchemy.Column
|
||||
constraints: List = []
|
||||
|
||||
primary_key: bool
|
||||
autoincrement: bool
|
||||
nullable: bool
|
||||
index: bool
|
||||
unique: bool
|
||||
pydantic_only: bool
|
||||
|
||||
default: Any
|
||||
server_default: Any
|
||||
|
||||
@classmethod
|
||||
def is_required(cls) -> bool:
|
||||
return (
|
||||
not cls.nullable and not cls.has_default() and not cls.is_auto_primary_key()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def default_value(cls):
|
||||
if cls.is_auto_primary_key():
|
||||
return Field(default=None)
|
||||
if cls.has_default():
|
||||
default = cls.default if cls.default is not None else cls.server_default
|
||||
if callable(default):
|
||||
return Field(default_factory=default)
|
||||
else:
|
||||
return Field(default=default)
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def has_default(cls):
|
||||
return cls.default is not None or cls.server_default is not None
|
||||
|
||||
@classmethod
|
||||
def is_auto_primary_key(cls) -> bool:
|
||||
if cls.primary_key:
|
||||
return cls.autoincrement
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def get_column(cls, name: str) -> sqlalchemy.Column:
|
||||
return sqlalchemy.Column(
|
||||
name,
|
||||
cls.column_type,
|
||||
*cls.constraints,
|
||||
primary_key=cls.primary_key,
|
||||
nullable=cls.nullable and not cls.primary_key,
|
||||
index=cls.index,
|
||||
unique=cls.unique,
|
||||
default=cls.default,
|
||||
server_default=cls.server_default,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def expand_relationship(cls, value: Any, child: "Model") -> Any:
|
||||
return value
|
||||
Reference in New Issue
Block a user