fix json fields and fix choices validation
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
import itertools
|
||||
import sqlite3
|
||||
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type
|
||||
from typing import Any, Dict, List, TYPE_CHECKING, Tuple, Type
|
||||
|
||||
from pydantic.typing import ForwardRef
|
||||
import ormar # noqa: I100
|
||||
from ormar.fields.foreign_key import ForeignKeyField
|
||||
from ormar.models.helpers.pydantic import populate_pydantic_default_values
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
@ -22,7 +21,7 @@ def is_field_an_forward_ref(field: Type["BaseField"]) -> bool:
|
||||
:return: result of the check
|
||||
:rtype: bool
|
||||
"""
|
||||
return issubclass(field, ForeignKeyField) and (
|
||||
return issubclass(field, ormar.ForeignKeyField) and (
|
||||
field.to.__class__ == ForwardRef or field.through.__class__ == ForwardRef
|
||||
)
|
||||
|
||||
@ -124,43 +123,6 @@ def extract_annotations_and_default_vals(attrs: Dict) -> Tuple[Dict, Dict]:
|
||||
return attrs, model_fields
|
||||
|
||||
|
||||
# cannot be in relations helpers due to cyclical import
|
||||
def validate_related_names_in_relations( # noqa CCR001
|
||||
model_fields: Dict, new_model: Type["Model"]
|
||||
) -> None:
|
||||
"""
|
||||
Performs a validation of relation_names in relation fields.
|
||||
If multiple fields are leading to the same related model
|
||||
only one can have empty related_name param
|
||||
(populated by default as model.name.lower()+'s').
|
||||
Also related_names have to be unique for given related model.
|
||||
|
||||
:raises ModelDefinitionError: if validation of related_names fail
|
||||
:param model_fields: dictionary of declared ormar model fields
|
||||
:type model_fields: Dict[str, ormar.Field]
|
||||
:param new_model:
|
||||
:type new_model: Model class
|
||||
"""
|
||||
already_registered: Dict[str, List[Optional[str]]] = dict()
|
||||
for field in model_fields.values():
|
||||
if issubclass(field, ForeignKeyField):
|
||||
to_name = (
|
||||
field.to.get_name()
|
||||
if not field.to.__class__ == ForwardRef
|
||||
else str(field.to)
|
||||
)
|
||||
previous_related_names = already_registered.setdefault(to_name, [])
|
||||
if field.related_name in previous_related_names:
|
||||
raise ormar.ModelDefinitionError(
|
||||
f"Multiple fields declared on {new_model.get_name(lower=False)} "
|
||||
f"model leading to {field.to.get_name(lower=False)} model without "
|
||||
f"related_name property set. \nThere can be only one relation with "
|
||||
f"default/empty name: '{new_model.get_name() + 's'}'"
|
||||
f"\nTip: provide different related_name for FK and/or M2M fields"
|
||||
)
|
||||
previous_related_names.append(field.related_name)
|
||||
|
||||
|
||||
def group_related_list(list_: List) -> Dict:
|
||||
"""
|
||||
Translates the list of related strings into a dictionary.
|
||||
@ -191,3 +153,19 @@ def group_related_list(list_: List) -> Dict:
|
||||
else:
|
||||
result_dict.setdefault(key, []).extend(new)
|
||||
return {k: v for k, v in sorted(result_dict.items(), key=lambda item: len(item[1]))}
|
||||
|
||||
|
||||
def 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).
|
||||
|
||||
:param model: newly constructed model
|
||||
:type model: Model class
|
||||
:param field_name: name of the ormar field
|
||||
:type field_name: str
|
||||
:return: result of the check
|
||||
:rtype: bool
|
||||
"""
|
||||
return not hasattr(model.Meta, field_name) or not getattr(model.Meta, field_name)
|
||||
|
||||
Reference in New Issue
Block a user