add support for normal pydantic fields
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import warnings
|
||||
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Type, Union
|
||||
|
||||
import sqlalchemy
|
||||
@ -43,6 +44,14 @@ class BaseField(FieldInfo):
|
||||
self.index: bool = kwargs.pop("index", False)
|
||||
self.unique: bool = kwargs.pop("unique", False)
|
||||
self.pydantic_only: bool = kwargs.pop("pydantic_only", False)
|
||||
if self.pydantic_only:
|
||||
warnings.warn(
|
||||
"Parameter `pydantic_only` is deprecated and will "
|
||||
"be removed in one of the next releases.\n You can declare "
|
||||
"pydantic fields in a normal way. \n Check documentation: "
|
||||
"https://collerek.github.io/ormar/fields/pydantic-fields",
|
||||
DeprecationWarning,
|
||||
)
|
||||
self.choices: typing.Sequence = kwargs.pop("choices", False)
|
||||
|
||||
self.virtual: bool = kwargs.pop(
|
||||
|
||||
@ -98,6 +98,7 @@ def get_pydantic_base_orm_config() -> Type[pydantic.BaseConfig]:
|
||||
|
||||
class Config(pydantic.BaseConfig):
|
||||
orm_mode = True
|
||||
validate_assignment = True
|
||||
|
||||
return Config
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@ from typing import (
|
||||
Sequence,
|
||||
Set,
|
||||
TYPE_CHECKING,
|
||||
Tuple,
|
||||
Type,
|
||||
Union,
|
||||
cast,
|
||||
@ -150,6 +151,13 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
k,
|
||||
self.Meta.model_fields[k].expand_relationship(
|
||||
v, self, to_register=False,
|
||||
)
|
||||
if k in self.Meta.model_fields
|
||||
else (
|
||||
v
|
||||
if k in self.__fields__
|
||||
# some random key will raise KeyError
|
||||
else self.__fields__["_Q*DHPQ(JAS*((JA)###*(&"]
|
||||
),
|
||||
"dumps",
|
||||
)
|
||||
@ -243,7 +251,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
else:
|
||||
if name in object.__getattribute__(self, "_choices_fields"):
|
||||
validate_choices(field=self.Meta.model_fields[name], value=value)
|
||||
super().__setattr__(name, value)
|
||||
super().__setattr__(name, self._convert_json(name, value, op="dumps"))
|
||||
self.set_save_status(False)
|
||||
|
||||
def __getattribute__(self, item: str) -> Any: # noqa: CCR001
|
||||
|
||||
Reference in New Issue
Block a user