diff --git a/.gitignore b/.gitignore index 03bfd0b..486dd59 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .vscode .venv .devcontainer +.idea # Python *.py[cod] diff --git a/src/drf_pydantic/parse.py b/src/drf_pydantic/parse.py index 536e08f..fd4ef5d 100644 --- a/src/drf_pydantic/parse.py +++ b/src/drf_pydantic/parse.py @@ -135,6 +135,13 @@ def _convert_field(field: pydantic.fields.FieldInfo) -> serializers.Field: if _default_value is not pydantic_core.PydanticUndefined: drf_field_kwargs["default"] = _default_value + # Adding description as help_text + if ( + field.description is not pydantic_core.PydanticUndefined + and field.description is not None + ): + drf_field_kwargs["help_text"] = field.description + # 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 3125335..b28193d 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -573,6 +573,8 @@ class Person(BaseModel): field_5: typing.Optional[str] field_6: typing.Optional[str] = "default" 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") serializer = Person.drf_serializer() @@ -600,6 +602,11 @@ class Person(BaseModel): assert serializer.fields["field_6"].allow_null is True assert serializer.fields["field_7"].allow_null is True + assert serializer.fields["field_6"].help_text is None + assert serializer.fields["field_7"].help_text is None + assert serializer.fields["field_8"].help_text == "8th field" + assert serializer.fields["field_9"].help_text == "9th field" + class TestManualFields: def test_same_type(self):