Skip to content

fix: normalize Field.verbose_name #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions litestar_django/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def get_field_type(cls, field: Field, type_map: dict[type[AnyField], Any]) -> An
def get_field_constraints(cls, field: AnyField) -> KwargDefinition:
constraints = {}
if isinstance(field, Field):
constraints["title"] = field.verbose_name
constraints["title"] = (
str(field.verbose_name) if field.verbose_name else field.name
) # might be a proxy

if field.help_text:
constraints["description"] = field.help_text
Expand Down Expand Up @@ -153,7 +155,7 @@ def get_field_constraints(cls, field: AnyField) -> KwargDefinition:
else:
constraints["title"] = field.name

return KwargDefinition(**constraints)
return KwargDefinition(**constraints) # type: ignore[arg-type]

@classmethod
def create_constraints_for_validator(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "litestar-django"
version = "0.1.0"
version = "0.1.1"
description = "Django model support for Litestar"
readme = "README.md"
license = { text = "MIT" }
Expand Down
6 changes: 6 additions & 0 deletions tests/some_app/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import enumfields
from django.core import validators
from django.db import models
from django.utils.translation import gettext


class StdEnum(str, enum.Enum):
Expand Down Expand Up @@ -117,6 +118,11 @@ class ModelWithFields(models.Model):
max_length=100,
)

field_with_non_string_verbose_name = models.CharField(
max_length=100,
verbose_name=gettext("Some field"),
)


class ModelInvalidRegexValidator(models.Model):
invalid_regex_validator = models.CharField(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_dto_field_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,22 @@ def test_basic_field_types() -> None:
),
)

assert field_defs[
"field_with_non_string_verbose_name"
] == DTOFieldDefinition.from_field_definition(
model_name="ModelWithFields",
default_factory=None,
dto_field=DTOField(),
field_definition=FieldDefinition.from_annotation(
str,
name="field_with_non_string_verbose_name",
kwarg_definition=KwargDefinition(
title="Some field",
max_length=100,
),
),
)


def test_invalid_regex_validator() -> None:
dto_type = DjangoModelDTO[ModelInvalidRegexValidator]
Expand Down
Loading