Skip to content

models.helpers.pydantic

create_pydantic_field

1
create_pydantic_field(field_name: str, model: Type["Model"], model_field: "ManyToManyField") -> None

Registers pydantic field on through model that leads to passed model and is registered as field_name passed.

Through model is fetched from through attributed on passed model_field.

Arguments:

  • field_name: field name to register :type field_name: str
  • model: type of field to register :type model: Model class
  • model_field: relation field from which through model is extracted :type model_field: ManyToManyField class

get_pydantic_field

1
get_pydantic_field(field_name: str, model: Type["Model"]) -> "ModelField"

Extracts field type and if it's required from Model model_fields by passed field_name. Returns a pydantic field with type of field_name field type.

Arguments:

  • field_name: field name to fetch from Model and name of pydantic field :type field_name: str
  • model: type of field to register :type model: Model class

Returns:

newly created pydantic field :rtype: pydantic.ModelField

populate_pydantic_default_values

1
populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]

Extracts ormar fields from annotations (deprecated) and from namespace dictionary of the class. Fields declared on model are all subclasses of the BaseField class.

Trigger conversion of ormar field into pydantic FieldInfo, which has all needed parameters saved.

Overwrites the annotations of ormar fields to corresponding types declared on ormar fields (constructed dynamically for relations). Those annotations are later used by pydantic to construct it's own fields.

Arguments:

  • attrs: current class namespace :type attrs: Dict

Returns:

namespace of the class updated, dict of extracted model_fields :rtype: Tuple[Dict, Dict]

merge_or_generate_pydantic_config

1
merge_or_generate_pydantic_config(attrs: Dict, name: str) -> None

Checks if the user provided pydantic Config, and if he did merges it with the default one.

Updates the attrs in place with a new config.

:rtype: None

get_pydantic_base_orm_config

1
get_pydantic_base_orm_config() -> Type[pydantic.BaseConfig]

Returns empty pydantic Config with orm_mode set to True.

Returns:

empty default config with orm_mode set. :rtype: pydantic Config

get_potential_fields

1
get_potential_fields(attrs: Dict) -> Dict

Gets all the fields in current class namespace that are Fields.

Arguments:

  • attrs: current class namespace :type attrs: Dict

Returns:

extracted fields that are ormar Fields :rtype: Dict

remove_excluded_parent_fields

1
remove_excluded_parent_fields(model: Type["Model"]) -> None

Removes pydantic fields that should be excluded from parent models

Arguments:

  • model: :type model: Type["Model"]