Fixed #29974 -- Fixed non-truthy primary key values for QuerySet.bulk_update().

This commit is contained in:
Tom Forbes 2018-11-22 03:58:43 +00:00 committed by Tim Graham
parent 5a71bd7f9d
commit 31408446b3
3 changed files with 10 additions and 2 deletions

View File

@ -490,7 +490,7 @@ class QuerySet:
if not fields: if not fields:
raise ValueError('Field names must be given to bulk_update().') raise ValueError('Field names must be given to bulk_update().')
objs = tuple(objs) objs = tuple(objs)
if not all(obj.pk for obj in objs): if any(obj.pk is None for obj in objs):
raise ValueError('All bulk_update() objects must have a primary key set.') raise ValueError('All bulk_update() objects must have a primary key set.')
fields = [self.model._meta.get_field(name) for name in fields] fields = [self.model._meta.get_field(name) for name in fields]
if any(not f.concrete or f.many_to_many for f in fields): if any(not f.concrete or f.many_to_many for f in fields):

View File

@ -591,6 +591,7 @@ class MyObject(models.Model):
class Order(models.Model): class Order(models.Model):
id = models.IntegerField(primary_key=True) id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=12, null=True, default='')
class Meta: class Meta:
ordering = ('pk',) ordering = ('pk',)

View File

@ -7,7 +7,7 @@ from django.test import TestCase
from .models import ( from .models import (
Article, CustomDbColumn, CustomPk, Detail, Individual, Member, Note, Article, CustomDbColumn, CustomPk, Detail, Individual, Member, Note,
Number, Paragraph, SpecialCategory, Tag, Valid, Number, Order, Paragraph, SpecialCategory, Tag, Valid,
) )
@ -167,6 +167,13 @@ class BulkUpdateTests(TestCase):
[cat.extra for cat in custom_pks] [cat.extra for cat in custom_pks]
) )
def test_falsey_pk_value(self):
order = Order.objects.create(pk=0, name='test')
order.name = 'updated'
Order.objects.bulk_update([order], ['name'])
order.refresh_from_db()
self.assertEqual(order.name, 'updated')
def test_inherited_fields(self): def test_inherited_fields(self):
special_categories = [ special_categories = [
SpecialCategory.objects.create(name=str(i), special_name=str(i)) SpecialCategory.objects.create(name=str(i), special_name=str(i))