Skip to content

Commit 8fd31ee

Browse files
authored
Merge pull request #1 from eadwinCode/m2mlink_validation_fix
M2MLink Validation Fix
2 parents 59f0157 + ff3a10a commit 8fd31ee

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

ninja_schema/orm/utils/converter.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,21 @@ def convert_django_field(field: Field, **kwargs: Any) -> Tuple[Type, FieldInfo]:
119119

120120

121121
@no_type_check
122-
def create_m2m_link_type(type_: Type[TModel]) -> Type[TModel]:
122+
def create_m2m_link_type(
123+
type_: Type[TModel], related_model: models.Model
124+
) -> Type[TModel]:
123125
class M2MLink(type_): # type: ignore
124126
@classmethod
125127
def __get_validators__(cls):
126128
yield cls.validate
127129

128130
@classmethod
129131
def validate(cls, v):
130-
return v.pk
132+
if isinstance(v, type_):
133+
return v
134+
if hasattr(v, "pk") and isinstance(v.pk, type_):
135+
return v.pk
136+
raise Exception("Invalid type")
131137

132138
return M2MLink
133139

@@ -179,7 +185,7 @@ def construct_relational_field_info(
179185

180186
python_type = inner_type
181187
if field.one_to_many or field.many_to_many:
182-
m2m_type = create_m2m_link_type(inner_type)
188+
m2m_type = create_m2m_link_type(inner_type, field.related_model)
183189
python_type = List[m2m_type] # type: ignore
184190

185191
field_info = FieldInfo(

0 commit comments

Comments
 (0)