django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

Improves create_schema to get types from custom_fields

Open lucasrcezimbra opened this issue 1 year ago • 2 comments

Related to #335, #694, #764

Removes the need to update TYPES for fields passed as custom_fields.

Example:

class CustomField(models.fields.Field):
    pass

class TestModel(models.Model):
    custom = CustomField()

CustomType = TypeVar("CustomType")

# before
TYPES.update({'CustomField': CustomType})
Schema1 = create_schema(TestModel, custom_fields=[("custom", CustomType, ...)])

# with this PR
Schema1 = create_schema(TestModel, custom_fields=[("custom", CustomType, ...)])

lucasrcezimbra avatar Oct 11 '23 12:10 lucasrcezimbra

Hi @lucasrcezimbra

Thank you for PR

Maybe it's also make sense to add some builtin method like "register_field_type("CustomField", MyPydanticType) that will update TYPES

and then error message should suggest to use this function ?

vitalik avatar Oct 11 '23 13:10 vitalik

Maybe it's also make sense to add some builtin method like "register_field_type("CustomField", MyPydanticType) that will update TYPES

and then error message should suggest to use this function ?

Makes sense. I'll add it to this PR.

lucasrcezimbra avatar Oct 12 '23 18:10 lucasrcezimbra