9.6 KiB
models.metaclass
PARSED_FIELDS_KEY
CONFIG_KEY
ModelMeta Objects
class ModelMeta()
Class used for type hinting. Users can subclass this one for convenience but it's not required. The only requirement is that ormar.Model has to have inner class with name Meta.
tablename
table
metadata
database
columns
constraints
pkname
model_fields
alias_manager
property_fields
signals
abstract
check_if_field_has_choices
check_if_field_has_choices(field: Type[BaseField]) -> bool
Checks if given field has choices populated. A if it has one, a validator for this field needs to be attached.
Arguments:
field (BaseField): ormar field to check
Returns:
(bool): result of the check
choices_validator
choices_validator(cls: Type["Model"], values: Dict[str, Any]) -> Dict[str, Any]
Validator that is attached to pydantic model pre root validators. Validator checks if field value is in field.choices list.
Raises:
ValueError: if field value is outside of allowed choices.
Arguments:
cls (Model class): constructed classvalues (Dict[str, Any]): dictionary of field values (pydantic side)
Returns:
(Dict[str, Any]): values if pass validation, otherwise exception is raised
populate_choices_validators
populate_choices_validators(model: Type["Model"]) -> None
Checks if Model has any fields with choices set. If yes it adds choices validation into pre root validators.
Arguments:
model (Model class): newly constructed Model
add_cached_properties
add_cached_properties(new_model: Type["Model"]) -> None
Sets cached properties for both pydantic and ormar models.
Quick access fields are fields grabbed in getattribute to skip all checks.
Related fields and names are populated to None as they can change later. When children models are constructed they can modify parent to register itself.
All properties here are used as "cache" to not recalculate them constantly.
Arguments:
new_model (Model class): newly constructed Model
meta_field_not_set
meta_field_not_set(model: Type["Model"], field_name: str) -> bool
Checks if field with given name is already present in model.Meta. Then check if it's set to something truthful (in practice meaning not None, as it's non or ormar Field only).
Arguments:
model (Model class): newly constructed modelfield_name (str): name of the ormar field
Returns:
(bool): result of the check
add_property_fields
add_property_fields(new_model: Type["Model"], attrs: Dict) -> None
Checks class namespace for properties or functions with property_field. If attribute have property_field it was decorated with @property_field.
Functions like this are exposed in dict() (therefore also fastapi result). Names of property fields are cached for quicker access / extraction.
Arguments:
new_model (Model class): newly constructed modelattrs (Dict[str, str]):
register_signals
register_signals(new_model: Type["Model"]) -> None
Registers on model's SignalEmmiter and sets pre defined signals. Predefined signals are (pre/post) + (save/update/delete).
Signals are emitted in both model own methods and in selected queryset ones.
Arguments:
new_model (Model class): newly constructed model
update_attrs_and_fields
update_attrs_and_fields(attrs: Dict, new_attrs: Dict, model_fields: Dict, new_model_fields: Dict, new_fields: Set) -> Dict
Updates annotations, values of model fields (so pydantic FieldInfos) as well as model.Meta.model_fields definitions from parents.
Arguments:
attrs (Dict): new namespace for class being constructednew_attrs (Dict): part of the namespace extracted from parent classmodel_fields (Dict[str, BaseField]): ormar fields in defined in current classnew_model_fields (Dict[str, BaseField]): ormar fields defined in parent classesnew_fields (Set[str]): set of new fields names
verify_constraint_names
verify_constraint_names(base_class: "Model", model_fields: Dict, parent_value: List) -> None
Verifies if redefined fields that are overwritten in subclasses did not remove any name of the column that is used in constraint as it will fail in sqlalchemy Table creation.
Arguments:
base_class (Model or model parent class): one of the parent classesmodel_fields (Dict[str, BaseField]): ormar fields in defined in current classparent_value (List): list of base class constraints
update_attrs_from_base_meta
update_attrs_from_base_meta(base_class: "Model", attrs: Dict, model_fields: Dict) -> None
Updates Meta parameters in child from parent if needed.
Arguments:
base_class (Model or model parent class): one of the parent classesattrs (Dict): new namespace for class being constructedmodel_fields (Dict[str, BaseField]): ormar fields in defined in current class
copy_data_from_parent_model
copy_data_from_parent_model(base_class: Type["Model"], curr_class: type, attrs: Dict, model_fields: Dict[
str, Union[Type[BaseField], Type[ForeignKeyField], Type[ManyToManyField]]
]) -> Tuple[Dict, Dict]
Copy the key parameters [databse, metadata, property_fields and constraints] and fields from parent models. Overwrites them if needed.
Only abstract classes can be subclassed.
Since relation fields requires different related_name for different children
Raises:
ModelDefinitionError: if non abstract model is subclassed
Arguments:
base_class (Model or model parent class): one of the parent classescurr_class (Model or model parent class): current constructed classattrs (Dict): new namespace for class being constructedmodel_fields (Dict[str, BaseField]): ormar fields in defined in current class
Returns:
(Tuple[Dict, Dict]): updated attrs and model_fields
extract_from_parents_definition
extract_from_parents_definition(base_class: type, curr_class: type, attrs: Dict, model_fields: Dict[
str, Union[Type[BaseField], Type[ForeignKeyField], Type[ManyToManyField]]
]) -> Tuple[Dict, Dict]
Extracts fields from base classes if they have valid oramr fields.
If model was already parsed -> fields definitions need to be removed from class cause pydantic complains about field re-definition so after first child we need to extract from parsed_fields not the class itself.
If the class is parsed first time annotations and field definition is parsed from the class.dict.
If the class is a ormar.Model it is skipped.
Arguments:
base_class (Model or model parent class): one of the parent classescurr_class (Model or model parent class): current constructed classattrs (Dict): new namespace for class being constructedmodel_fields (Dict[str, BaseField]): ormar fields in defined in current class
Returns:
(Tuple[Dict, Dict]): updated attrs and model_fields
ModelMetaclass Objects
class ModelMetaclass(pydantic.main.ModelMetaclass)
__new__
| __new__(mcs: "ModelMetaclass", name: str, bases: Any, attrs: dict) -> "ModelMetaclass"
Metaclass used by ormar Models that performs configuration and build of ormar Models.
Sets pydantic configuration. Extract model_fields and convert them to pydantic FieldInfo, updates class namespace.
Extracts settings and fields from parent classes. Fetches methods decorated with @property_field decorator to expose them later in dict().
Construct parent pydantic Metaclass/ Model.
If class has Meta class declared (so actual ormar Models) it also:
- populate sqlalchemy columns, pkname and tables from model_fields
- register reverse relationships on related models
- registers all relations in alias manager that populates table_prefixes
- exposes alias manager on each Model
- creates QuerySet for each model and exposes it on a class
Arguments:
name (str): name of current classbases (Tuple): base classesattrs (Dict): class namespace