diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-11-22 14:26:23 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-11-22 14:26:48 +0100 |
| commit | 3b0a8ea2993a2b011ad57304b3f978947adb5981 (patch) | |
| tree | ac14210e13a838fb8758e22807e670292d3eef50 /django | |
| parent | 075a4b51c7a221bdb01736b0d3e82bc54869f7ab (diff) | |
[4.1.x] Fixed #34177 -- Fixed QuerySet.bulk_create() crash on "pk" in unique_fields.
Bug in 0f6946495a8ec955b471ca1baaf408ceb53d4796.
Backport of 7d5329852f19c6ae78c6f6f3d3e41835377bf295 from main
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/query.py | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index b00a7f3325..cd14b3a67e 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -728,11 +728,8 @@ class QuerySet: "update_fields." ) if unique_fields: - # Primary key is allowed in unique_fields. unique_fields = [ - self.model._meta.get_field(name) - for name in unique_fields - if name != "pk" + self.model._meta.get_field(name) for name in unique_fields ] if any(not f.concrete or f.many_to_many for f in unique_fields): raise ValueError( @@ -781,6 +778,12 @@ class QuerySet: raise ValueError("Can't bulk create a multi-table inherited model") if not objs: return objs + opts = self.model._meta + if unique_fields: + # Primary key is allowed in unique_fields. + unique_fields = [ + opts.pk.name if name == "pk" else name for name in unique_fields + ] on_conflict = self._check_bulk_create_options( ignore_conflicts, update_conflicts, @@ -788,7 +791,6 @@ class QuerySet: unique_fields, ) self._for_write = True - opts = self.model._meta fields = opts.concrete_fields objs = list(objs) self._prepare_for_bulk_create(objs) |
