diff --git a/src/drf_pydantic/parse.py b/src/drf_pydantic/parse.py index fd4ef5d..5f33e46 100644 --- a/src/drf_pydantic/parse.py +++ b/src/drf_pydantic/parse.py @@ -142,6 +142,10 @@ def _convert_field(field: pydantic.fields.FieldInfo) -> serializers.Field: ): drf_field_kwargs["help_text"] = field.description + # Adding label as title + if field.title is not pydantic_core.PydanticUndefined and field.title is not None: + drf_field_kwargs["label"] = field.title + # Process constraints for item in field.metadata: if isinstance(item, pydantic.StringConstraints): diff --git a/tests/test_fields.py b/tests/test_fields.py index b28193d..446c2df 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -575,6 +575,8 @@ class Person(BaseModel): field_7: typing.Optional[str] = None field_8: typing.Annotated[str, pydantic.Field(description="8th field")] field_9: str = pydantic.Field(default="default", description="9th field") + field_10: str = pydantic.Field(default="default", title="10th field") + field_11: typing.Annotated[str, pydantic.Field(title="11th field")] serializer = Person.drf_serializer() @@ -607,6 +609,14 @@ class Person(BaseModel): assert serializer.fields["field_8"].help_text == "8th field" assert serializer.fields["field_9"].help_text == "9th field" + # Field 8, Field 9 is automatically generated label + assert serializer.fields["field_8"].label == "Field 8" + assert serializer.fields["field_9"].label == "Field 9" + + # This is custom defined label + assert serializer.fields["field_10"].label == "10th field" + assert serializer.fields["field_11"].label == "11th field" + class TestManualFields: def test_same_type(self):