Models#
EzyCore uses Pydantic for handling its models,
this provides us with many advantages such as stronger validation methods.
All Model like objects in the EzyCore library must inherit the Model class.
What is the Model class?#
The Model class is simply an extended version of pydantic’s BaseModel class,
It offers all the same features as it does and works virtually the same.
What does the Model class do?#
Ensures a
_configvariable exists, which helps customise how segments/managers behaveVerifies all partial references within an object
Controlling BaseSegment.get()#
[BaseSegment].get(..., '*')
# Ignores any other kwds provided!!!
[BaseSegment].get(..., 'f1', 'f2')
[BaseSegment].get(..., ('f1', {'f2': True}), 'f3')
[BaseSegment].get(..., exclude_none=True)
# Ignore any fields set to "None"
[BaseSegment].get(..., 'f1', include={'f1', 'f2'})
# Ignores any args in the "*include" field instead uses the include kwarg
Examples#
Basic#
A basic user object which excludes fields _config and password when fetched
class User(Model):
id: int
username: str
password: str
created_at: datetime.datetime
updated_at: datetime.datetime
_config: Config = dict(search_by='id', exclude={'password'})
Intermediate#
Implements partial referencing to fetch results from the users segment
class Token(Model):
id: int
limit: int
used: int
created_at: datetime.datetime
updated_at: datetime.datetime
owner: PartialRef[User]
_config: Config = dict(
search_by='id',
exclude={'owner': {'password'}}
partials={'owner': 'users'})
References#
Config#
-
class
ezycore.models.Config(*, search_by: str, exclude: Union[dict, set] = {}, partials: Dict[str, str] = {}, invalidate_after: int = -1)[source]# -
Configuration class used by the ezycore module. Used to customise and control how ezycore behaves with segments and models
- Parameters:
search_by (
str) –Which key to store as the primary key
Warning
This field MUST be UNIQUE
exclude (Union[
dict,set]) – Fields to exclude from being returned when being fetchedpartials (Dict[
str,str]) – Mapping of partial vars to segment names.invalidate_after (
int) – Automatically invalidates entry after it is fetched n times
-
classmethod
construct(_fields_set: Optional[SetStr] = None, **values: Any) Model# -
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data. Default values are respected, but no other validation is performed. Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
-
copy(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, update: Optional[DictStrAny] = None, deep: bool = False) Model# -
Duplicate a model, optionally choose which fields to include, exclude and change.
- Parameters:
include – fields to include in new model
exclude – fields to exclude from new model, as with values this takes precedence over include
update – values to change/add in the new model. Note: the data is not validated before creating the new model: you should trust this data
deep – set to True to make a deep copy of the model
- Returns:
new model instance
-
dict(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, by_alias: bool = False, skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False) DictStrAny# -
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
-
json(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, by_alias: bool = False, skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, encoder: Optional[Callable[[Any], Any]] = None, models_as_dict: bool = True, **dumps_kwargs: Any) unicode# -
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
-
classmethod
update_forward_refs(**localns: Any) None# -
Try to update ForwardRefs on fields based on this Model, globalns and localns.
Model#
-
class
ezycore.models.Model[source]# -
-
classmethod
construct(_fields_set: Optional[SetStr] = None, **values: Any) Model# -
Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data. Default values are respected, but no other validation is performed. Behaves as if Config.extra = ‘allow’ was set since it adds all passed values
-
copy(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, update: Optional[DictStrAny] = None, deep: bool = False) Model# -
Duplicate a model, optionally choose which fields to include, exclude and change.
- Parameters:
include – fields to include in new model
exclude – fields to exclude from new model, as with values this takes precedence over include
update – values to change/add in the new model. Note: the data is not validated before creating the new model: you should trust this data
deep – set to True to make a deep copy of the model
- Returns:
new model instance
-
dict(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, by_alias: bool = False, skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False) DictStrAny# -
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
-
json(*, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] = None, by_alias: bool = False, skip_defaults: Optional[bool] = None, exclude_unset: bool = False, exclude_defaults: bool = False, exclude_none: bool = False, encoder: Optional[Callable[[Any], Any]] = None, models_as_dict: bool = True, **dumps_kwargs: Any) unicode# -
Generate a JSON representation of the model, include and exclude arguments as per dict().
encoder is an optional function to supply as default to json.dumps(), other arguments as per json.dumps().
-
classmethod
update_forward_refs(**localns: Any) None# -
Try to update ForwardRefs on fields based on this Model, globalns and localns.
-
classmethod
