-
-
Notifications
You must be signed in to change notification settings - Fork 505
Validation of the fields
argument to bulk_update
#2808
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
base: master
Are you sure you want to change the base?
Validation of the fields
argument to bulk_update
#2808
Conversation
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()) |
There was a problem hiding this comment.
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"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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] |
There was a problem hiding this comment.
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
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