mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #24997 -- Enabled bulk_create() on proxy models
This commit is contained in:
parent
fedef7b2c6
commit
9a5cfa05a0
6 changed files with 68 additions and 18 deletions
|
@ -6,6 +6,25 @@ class Country(models.Model):
|
|||
iso_two_letter = models.CharField(max_length=2)
|
||||
|
||||
|
||||
class ProxyCountry(Country):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
class ProxyProxyCountry(ProxyCountry):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
class ProxyMultiCountry(ProxyCountry):
|
||||
pass
|
||||
|
||||
|
||||
class ProxyMultiProxyCountry(ProxyMultiCountry):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
|
||||
class Place(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
|
||||
|
|
|
@ -7,7 +7,10 @@ from django.test import (
|
|||
TestCase, override_settings, skipIfDBFeature, skipUnlessDBFeature,
|
||||
)
|
||||
|
||||
from .models import Country, Pizzeria, Restaurant, State, TwoFields
|
||||
from .models import (
|
||||
Country, Pizzeria, ProxyCountry, ProxyMultiCountry, ProxyMultiProxyCountry,
|
||||
ProxyProxyCountry, Restaurant, State, TwoFields,
|
||||
)
|
||||
|
||||
|
||||
class BulkCreateTests(TestCase):
|
||||
|
@ -35,21 +38,36 @@ class BulkCreateTests(TestCase):
|
|||
with self.assertNumQueries(1):
|
||||
Country.objects.bulk_create(self.data)
|
||||
|
||||
def test_inheritance(self):
|
||||
Restaurant.objects.bulk_create([
|
||||
Restaurant(name="Nicholas's")
|
||||
])
|
||||
self.assertQuerysetEqual(Restaurant.objects.all(), [
|
||||
"Nicholas's",
|
||||
], attrgetter("name"))
|
||||
with self.assertRaises(ValueError):
|
||||
def test_multi_table_inheritance_unsupported(self):
|
||||
expected_message = "Can't bulk create a multi-table inherited model"
|
||||
with self.assertRaisesMessage(ValueError, expected_message):
|
||||
Pizzeria.objects.bulk_create([
|
||||
Pizzeria(name="The Art of Pizza")
|
||||
Pizzeria(name="The Art of Pizza"),
|
||||
])
|
||||
self.assertQuerysetEqual(Pizzeria.objects.all(), [])
|
||||
self.assertQuerysetEqual(Restaurant.objects.all(), [
|
||||
"Nicholas's",
|
||||
], attrgetter("name"))
|
||||
with self.assertRaisesMessage(ValueError, expected_message):
|
||||
ProxyMultiCountry.objects.bulk_create([
|
||||
ProxyMultiCountry(name="Fillory", iso_two_letter="FL"),
|
||||
])
|
||||
with self.assertRaisesMessage(ValueError, expected_message):
|
||||
ProxyMultiProxyCountry.objects.bulk_create([
|
||||
ProxyMultiProxyCountry(name="Fillory", iso_two_letter="FL"),
|
||||
])
|
||||
|
||||
def test_proxy_inheritance_supported(self):
|
||||
ProxyCountry.objects.bulk_create([
|
||||
ProxyCountry(name="Qwghlm", iso_two_letter="QW"),
|
||||
Country(name="Tortall", iso_two_letter="TA"),
|
||||
])
|
||||
self.assertQuerysetEqual(ProxyCountry.objects.all(), {
|
||||
"Qwghlm", "Tortall"
|
||||
}, attrgetter("name"), ordered=False)
|
||||
|
||||
ProxyProxyCountry.objects.bulk_create([
|
||||
ProxyProxyCountry(name="Neitherlands", iso_two_letter="NT"),
|
||||
])
|
||||
self.assertQuerysetEqual(ProxyProxyCountry.objects.all(), {
|
||||
"Qwghlm", "Tortall", "Neitherlands",
|
||||
}, attrgetter("name"), ordered=False)
|
||||
|
||||
def test_non_auto_increment_pk(self):
|
||||
State.objects.bulk_create([
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue