Skip to content

Commit 5412c62

Browse files
authored
Fix partial support (#7)
1 parent 287fec7 commit 5412c62

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

litestar_django/dto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_field_constraints(cls, field: AnyField) -> KwargDefinition:
120120
# fast path for known supported validators
121121
# nullable fields do not support these constraints and for enum the
122122
# constraint is defined implicitly by its values
123-
if not field.null and "enum" not in constraints:
123+
if not (field.null or "enum" in constraints or cls.config.partial):
124124
if isinstance(validator, validators.MinValueValidator):
125125
constraints["gt"] = validator.limit_value
126126
elif isinstance(validator, validators.MinLengthValidator):

tests/test_dto_field_definitions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,26 @@ def test_constraints() -> None:
401401
)
402402

403403

404+
def test_no_len_constraints_on_partial() -> None:
405+
dto_type = DjangoModelDTO[Annotated[ModelWithFields, DjangoDTOConfig(partial=True)]]
406+
field_defs = {
407+
f.name: f for f in dto_type.generate_field_definitions(ModelWithFields)
408+
}
409+
410+
assert field_defs["min_1_int_field"] == DTOFieldDefinition.from_field_definition(
411+
model_name="ModelWithFields",
412+
default_factory=None,
413+
dto_field=DTOField(),
414+
field_definition=FieldDefinition.from_annotation(
415+
int,
416+
name="min_1_int_field",
417+
kwarg_definition=KwargDefinition(
418+
title="min 1 int field",
419+
),
420+
),
421+
)
422+
423+
404424
def test_nullable_field() -> None:
405425
dto_type = DjangoModelDTO[ModelWithFields]
406426
field_defs = {

tests/test_dto_integration.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ def handler(data: Author) -> Author:
174174
assert res.json() == {"id": author.id, "name": author_name, "books": []}
175175

176176

177+
@pytest.mark.django_db(transaction=True)
178+
def test_validate_partial() -> None:
179+
@post(
180+
"/",
181+
sync_to_thread=True,
182+
dto=DjangoModelDTO[Annotated[Author, DTOConfig(partial=True)]],
183+
return_dto=DjangoModelDTO[Author],
184+
)
185+
def handler(data: Author) -> Author:
186+
data.save()
187+
return Author.objects.prefetch_related("books").get(id=data.id)
188+
189+
with create_test_client([handler]) as client:
190+
author_name = secrets.token_hex()
191+
res = client.post("/", json={"name": author_name})
192+
assert res.status_code == 201
193+
author = Author.objects.get(name=author_name)
194+
assert res.json() == {"id": author.id, "name": author_name, "books": []}
195+
196+
177197
@pytest.mark.django_db(transaction=True)
178198
def test_enumfields() -> None:
179199
@post(

0 commit comments

Comments
 (0)