Skip to content

Commit

Permalink
handle wrong type passed to validate relation
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdeldjalil-H committed Dec 19, 2024
1 parent e3f4f26 commit 6e39956
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tortoise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from tortoise.exceptions import (
ConfigurationError,
DoesNotExist,
FieldError,
IncompleteInstanceError,
IntegrityError,
ObjectDoesNotExistError,
Expand Down Expand Up @@ -814,7 +815,14 @@ def _validate_relation_type(cls, field_key: str, value: Optional["Model"]) -> No
if value is None:
return

expected_model = cls._meta.fields_map[field_key].related_model
field = cls._meta.fields_map[field_key]
if not isinstance(field, (OneToOneFieldInstance, ForeignKeyFieldInstance)):
raise FieldError(
f"Field '{field_key}' must be a OneToOne or ForeignKey relation, "
f"got {type(field).__name__}"
)

expected_model = field.related_model
received_model = type(value)
if received_model is not expected_model:
raise ValidationError(
Expand Down

0 comments on commit 6e39956

Please sign in to comment.