add excludes for pks and through models in dict
This commit is contained in:
@ -136,10 +136,10 @@ class String(ModelFieldFactory, str):
|
||||
def __new__( # type: ignore # noqa CFQ002
|
||||
cls,
|
||||
*,
|
||||
max_length: int,
|
||||
allow_blank: bool = True,
|
||||
strip_whitespace: bool = False,
|
||||
min_length: int = None,
|
||||
max_length: int = None,
|
||||
curtail_length: int = None,
|
||||
regex: str = None,
|
||||
**kwargs: Any
|
||||
@ -176,7 +176,7 @@ class String(ModelFieldFactory, str):
|
||||
:type kwargs: Any
|
||||
"""
|
||||
max_length = kwargs.get("max_length", None)
|
||||
if max_length is None or max_length <= 0:
|
||||
if max_length <= 0:
|
||||
raise ModelDefinitionError(
|
||||
"Parameter max_length is required for field String"
|
||||
)
|
||||
@ -435,7 +435,7 @@ class LargeBinary(ModelFieldFactory, bytes):
|
||||
_sample = "bytes"
|
||||
|
||||
def __new__( # type: ignore # noqa CFQ002
|
||||
cls, *, max_length: int = None, **kwargs: Any
|
||||
cls, *, max_length: int, **kwargs: Any
|
||||
) -> BaseField: # type: ignore
|
||||
kwargs = {
|
||||
**kwargs,
|
||||
@ -468,7 +468,7 @@ class LargeBinary(ModelFieldFactory, bytes):
|
||||
:type kwargs: Any
|
||||
"""
|
||||
max_length = kwargs.get("max_length", None)
|
||||
if max_length is None or max_length <= 0:
|
||||
if max_length <= 0:
|
||||
raise ModelDefinitionError(
|
||||
"Parameter max_length is required for field LargeBinary"
|
||||
)
|
||||
|
||||
@ -151,7 +151,7 @@ class ExcludableMixin(RelationMixin):
|
||||
:return: set or dict with excluded fields added.
|
||||
:rtype: Union[Set, Dict]
|
||||
"""
|
||||
exclude = exclude or {}
|
||||
exclude = exclude or set()
|
||||
related_set = cls.extract_related_names()
|
||||
if isinstance(exclude, set):
|
||||
exclude = {s for s in exclude}
|
||||
@ -162,6 +162,26 @@ class ExcludableMixin(RelationMixin):
|
||||
exclude = exclude.union(related_set)
|
||||
return exclude
|
||||
|
||||
@classmethod
|
||||
def _update_excluded_with_pks_and_through(
|
||||
cls, exclude: Set, exclude_primary_keys: bool, exclude_through_models: bool
|
||||
) -> Set:
|
||||
"""
|
||||
Updates excluded names with name of pk column if exclude flag is set.
|
||||
|
||||
:param exclude: set of names to exclude
|
||||
:type exclude: Set
|
||||
:param exclude_primary_keys: flag if the primary keys should be excluded
|
||||
:type exclude_primary_keys: bool
|
||||
:return: set updated with pk if flag is set
|
||||
:rtype: Set
|
||||
"""
|
||||
if exclude_primary_keys:
|
||||
exclude.add(cls.Meta.pkname)
|
||||
if exclude_through_models:
|
||||
exclude = exclude.union(cls.extract_through_names())
|
||||
return exclude
|
||||
|
||||
@classmethod
|
||||
def get_names_to_exclude(cls, excludable: ExcludableItems, alias: str) -> Set:
|
||||
"""
|
||||
|
||||
@ -562,6 +562,8 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
models: MutableSequence,
|
||||
include: Union[Set, Dict, None],
|
||||
exclude: Union[Set, Dict, None],
|
||||
exclude_primary_keys: bool,
|
||||
exclude_through_models: bool,
|
||||
) -> List:
|
||||
"""
|
||||
Converts list of models into list of dictionaries.
|
||||
@ -580,7 +582,11 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
try:
|
||||
result.append(
|
||||
model.dict(
|
||||
relation_map=relation_map, include=include, exclude=exclude,
|
||||
relation_map=relation_map,
|
||||
include=include,
|
||||
exclude=exclude,
|
||||
exclude_primary_keys=exclude_primary_keys,
|
||||
exclude_through_models=exclude_through_models,
|
||||
)
|
||||
)
|
||||
except ReferenceError: # pragma no cover
|
||||
@ -623,6 +629,8 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
dict_instance: Dict,
|
||||
include: Optional[Dict],
|
||||
exclude: Optional[Dict],
|
||||
exclude_primary_keys: bool,
|
||||
exclude_through_models: bool,
|
||||
) -> Dict:
|
||||
"""
|
||||
Traverse nested models and converts them into dictionaries.
|
||||
@ -655,6 +663,8 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
models=nested_model,
|
||||
include=self._convert_all(self._skip_ellipsis(include, field)),
|
||||
exclude=self._convert_all(self._skip_ellipsis(exclude, field)),
|
||||
exclude_primary_keys=exclude_primary_keys,
|
||||
exclude_through_models=exclude_through_models,
|
||||
)
|
||||
elif nested_model is not None:
|
||||
|
||||
@ -664,6 +674,8 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
),
|
||||
include=self._convert_all(self._skip_ellipsis(include, field)),
|
||||
exclude=self._convert_all(self._skip_ellipsis(exclude, field)),
|
||||
exclude_primary_keys=exclude_primary_keys,
|
||||
exclude_through_models=exclude_through_models,
|
||||
)
|
||||
else:
|
||||
dict_instance[field] = None
|
||||
@ -681,6 +693,8 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
exclude_unset: bool = False,
|
||||
exclude_defaults: bool = False,
|
||||
exclude_none: bool = False,
|
||||
exclude_primary_keys: bool = False,
|
||||
exclude_through_models: bool = False,
|
||||
relation_map: Dict = None,
|
||||
) -> "DictStrAny": # noqa: A003'
|
||||
"""
|
||||
@ -692,6 +706,10 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
|
||||
Additionally fields decorated with @property_field are also added.
|
||||
|
||||
:param exclude_through_models: flag to exclude through models from dict
|
||||
:type exclude_through_models: bool
|
||||
:param exclude_primary_keys: flag to exclude primary keys from dict
|
||||
:type exclude_primary_keys: bool
|
||||
:param include: fields to include
|
||||
:type include: Union[Set, Dict, None]
|
||||
:param exclude: fields to exclude
|
||||
@ -711,9 +729,15 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
pydantic_exclude = self._update_excluded_with_related(exclude)
|
||||
pydantic_exclude = self._update_excluded_with_pks_and_through(
|
||||
exclude=pydantic_exclude,
|
||||
exclude_primary_keys=exclude_primary_keys,
|
||||
exclude_through_models=exclude_through_models,
|
||||
)
|
||||
dict_instance = super().dict(
|
||||
include=include,
|
||||
exclude=self._update_excluded_with_related(exclude),
|
||||
exclude=pydantic_exclude,
|
||||
by_alias=by_alias,
|
||||
skip_defaults=skip_defaults,
|
||||
exclude_unset=exclude_unset,
|
||||
@ -738,6 +762,8 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
dict_instance=dict_instance,
|
||||
include=include, # type: ignore
|
||||
exclude=exclude, # type: ignore
|
||||
exclude_primary_keys=exclude_primary_keys,
|
||||
exclude_through_models=exclude_through_models,
|
||||
)
|
||||
|
||||
# include model properties as fields in dict
|
||||
|
||||
@ -893,7 +893,7 @@ class QuerySet(Generic[T]):
|
||||
"""
|
||||
Returns all rows from a database for given model for set filter options.
|
||||
|
||||
Passing args and/or kwargs is a shortcut and equals to calling
|
||||
Passing args and/or kwargs is a shortcut and equals to calling
|
||||
`filter(*args, **kwargs).all()`.
|
||||
|
||||
If there are no rows meeting the criteria an empty list is returned.
|
||||
|
||||
Reference in New Issue
Block a user