1
0
mirror of https://github.com/django/django.git synced 2025-10-29 16:46:11 +00:00

Fixed #31500 -- Fixed detecting of unique fields in QuerySet.in_bulk() when using Meta.constraints.

Detection of unique fields now takes into account non-partial unique
constraints.
This commit is contained in:
Hannes Ljungberg
2020-04-22 11:36:03 +02:00
committed by Mariusz Felisiak
parent 67f9d076cf
commit 447980e72a
3 changed files with 56 additions and 2 deletions

View File

@@ -689,7 +689,17 @@ class QuerySet:
"""
assert not self.query.is_sliced, \
"Cannot use 'limit' or 'offset' with in_bulk"
if field_name != 'pk' and not self.model._meta.get_field(field_name).unique:
opts = self.model._meta
unique_fields = [
constraint.fields[0]
for constraint in opts.total_unique_constraints
if len(constraint.fields) == 1
]
if (
field_name != 'pk' and
not opts.get_field(field_name).unique and
field_name not in unique_fields
):
raise ValueError("in_bulk()'s field_name must be a unique field but %r isn't." % field_name)
if id_list is not None:
if not id_list: