add extra to Model.Meta, update docs and bump version

This commit is contained in:
collerek
2021-09-26 14:07:47 +02:00
parent 67487ebf9c
commit 1da9111dbd
10 changed files with 158 additions and 4 deletions

View File

@ -64,7 +64,7 @@ from ormar.fields import (
UUID,
UniqueColumns,
) # noqa: I100
from ormar.models import ExcludableItems, Model
from ormar.models import ExcludableItems, Model, Extra
from ormar.models.metaclass import ModelMeta
from ormar.queryset import OrderAction, QuerySet, and_, or_
from ormar.relations import RelationType
@ -78,7 +78,7 @@ class UndefinedType: # pragma no cover
Undefined = UndefinedType()
__version__ = "0.10.19"
__version__ = "0.10.20"
__all__ = [
"Integer",
"BigInteger",
@ -130,4 +130,5 @@ __all__ = [
"ENCODERS_MAP",
"DECODERS_MAP",
"LargeBinary",
"Extra",
]

View File

@ -8,5 +8,6 @@ from ormar.models.newbasemodel import NewBaseModel # noqa I100
from ormar.models.model_row import ModelRow # noqa I100
from ormar.models.model import Model, T # noqa I100
from ormar.models.excludable import ExcludableItems # noqa I100
from ormar.models.utils import Extra # noqa I100
__all__ = ["NewBaseModel", "Model", "ModelRow", "ExcludableItems", "T"]
__all__ = ["NewBaseModel", "Model", "ModelRow", "ExcludableItems", "T", "Extra"]

View File

@ -7,6 +7,7 @@ import pydantic
from pydantic.typing import ForwardRef
import ormar # noqa: I100
from ormar.models.helpers.pydantic import populate_pydantic_default_values
from ormar.models.utils import Extra
if TYPE_CHECKING: # pragma no cover
from ormar import Model
@ -52,6 +53,8 @@ def populate_default_options_values(
new_model.Meta.model_fields = model_fields
if not hasattr(new_model.Meta, "abstract"):
new_model.Meta.abstract = False
if not hasattr(new_model.Meta, "extra"):
new_model.Meta.extra = Extra.forbid
if not hasattr(new_model.Meta, "orders_by"):
new_model.Meta.orders_by = []
if not hasattr(new_model.Meta, "exclude_parent_fields"):

View File

@ -49,6 +49,7 @@ from ormar.models.helpers import (
sqlalchemy_columns_from_model_fields,
)
from ormar.models.quick_access_views import quick_access_set
from ormar.models.utils import Extra
from ormar.queryset import FieldAccessor, QuerySet
from ormar.relations.alias_manager import AliasManager
from ormar.signals import Signal, SignalEmitter
@ -83,6 +84,7 @@ class ModelMeta:
requires_ref_update: bool
orders_by: List[str]
exclude_parent_fields: List[str]
extra: Extra
def add_cached_properties(new_model: Type["Model"]) -> None:

View File

@ -18,6 +18,8 @@ from typing import (
cast,
)
from ormar.models.utils import Extra
try:
import orjson as json
except ImportError: # pragma: no cover
@ -250,6 +252,12 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
for field_name in self.extract_through_names():
through_tmp_dict[field_name] = kwargs.pop(field_name, None)
if self.Meta.extra == Extra.ignore:
kwargs = {
k: v
for k, v in kwargs.items()
if k in model_fields or k in pydantic_fields
}
try:
new_kwargs: Dict[str, Any] = {
k: self._convert_to_bytes(

6
ormar/models/utils.py Normal file
View File

@ -0,0 +1,6 @@
from enum import Enum
class Extra(str, Enum):
ignore = "ignore"
forbid = "forbid"