add exclude_parent_fields param and first test
This commit is contained in:
@ -8,6 +8,7 @@ from ormar.models.helpers.pydantic import (
|
||||
get_potential_fields,
|
||||
get_pydantic_base_orm_config,
|
||||
get_pydantic_field,
|
||||
remove_excluded_parent_fields,
|
||||
)
|
||||
from ormar.models.helpers.relations import (
|
||||
alias_manager,
|
||||
@ -36,4 +37,5 @@ __all__ = [
|
||||
"sqlalchemy_columns_from_model_fields",
|
||||
"populate_choices_validators",
|
||||
"meta_field_not_set",
|
||||
"remove_excluded_parent_fields",
|
||||
]
|
||||
|
||||
@ -54,6 +54,8 @@ def populate_default_options_values(
|
||||
new_model.Meta.abstract = False
|
||||
if not hasattr(new_model.Meta, "orders_by"):
|
||||
new_model.Meta.orders_by = []
|
||||
if not hasattr(new_model.Meta, "exclude_parent_fields"):
|
||||
new_model.Meta.exclude_parent_fields = []
|
||||
|
||||
if any(
|
||||
is_field_an_forward_ref(field) for field in new_model.Meta.model_fields.values()
|
||||
|
||||
@ -117,3 +117,17 @@ def get_potential_fields(attrs: Dict) -> Dict:
|
||||
for k, v in attrs.items()
|
||||
if (lenient_issubclass(v, BaseField) or isinstance(v, BaseField))
|
||||
}
|
||||
|
||||
|
||||
def remove_excluded_parent_fields(model: Type["Model"]):
|
||||
"""
|
||||
Removes pydantic fields that should be excluded from parent models
|
||||
|
||||
:param model:
|
||||
:type model: Type["Model"]
|
||||
"""
|
||||
excludes = {*model.Meta.exclude_parent_fields} - {*model.Meta.model_fields.keys()}
|
||||
if excludes:
|
||||
model.__fields__ = {
|
||||
k: v for k, v in model.__fields__.items() if k not in excludes
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user