Skip to content

Conversation

UnknownPlatypus
Copy link
Contributor

I have made things!

Mostly mirrors validation logic from django.db.models.query.QuerySet.bulk_update.
Tests are inspired from django/tests/queries/test_bulk_update.py

I could have called a dummy bulk_update with no objects to reuse django internal logic but it felt brittle with risk of someday actually do database query so I preferred to re-implement the few checks

return False

if not field.concrete or field.many_to_many:
ctx.api.fail(f'bulk_update() can only be used with concrete fields. Got "{field_name}"', ctx.context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx.api.fail(f'bulk_update() can only be used with concrete fields. Got "{field_name}"', ctx.context)
ctx.api.fail(f'"bulk_update()" can only be used with concrete fields. Got "{field_name}"', ctx.context)

all_pk_fields.update(parent._meta.pk_fields)

if field in all_pk_fields:
ctx.api.fail(f'bulk_update() cannot be used with primary key fields. Got "{field_name}"', ctx.context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx.api.fail(f'bulk_update() cannot be used with primary key fields. Got "{field_name}"', ctx.context)
ctx.api.fail(f'"bulk_update()" cannot be used with primary key fields. Got "{field_name}"', ctx.context)

return ctx.default_return_type

if len(fields_args.items) == 0:
ctx.api.fail("Field names must be given to bulk_update()", ctx.context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx.api.fail("Field names must be given to bulk_update()", ctx.context)
ctx.api.fail('Field names must be given to "bulk_update()"', ctx.context)

def get_fields() -> list[str]:
return ["title"]
Article.objects.bulk_update(articles, get_fields())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, test at least one error with .abulk_update.
Do we have similar missing tests with other a methods?

Article.objects.bulk_update(articles, ["published"])
# Valid multiple field updates
Article.objects.bulk_update(articles, ["title", "content"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Article.objects.bulk_update(articles, ["title", "content"])
Article.objects.bulk_update(articles, ("title", "content"))

just for the variaty.

isinstance(ctx.type, Instance)
and (django_model := helpers.get_model_info_from_qs_ctx(ctx, django_context)) is not None
and len(ctx.args) >= 2
and ctx.args[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, don't forget to support TupleExpr and SetExpr here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants