Commit 9a5cfa05 authored by William Schwartz's avatar William Schwartz Committed by Tim Graham
Browse files

Fixed #24997 -- Enabled bulk_create() on proxy models

parent fedef7b2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -724,6 +724,7 @@ answer newbie questions, and generally made Django that much better:
    Wiktor Kołodziej <wiktor@pykonik.org>
    Wiley Kestner <wiley.kestner@gmail.com>
    Wiliam Alves de Souza <wiliamsouza83@gmail.com>
    William Schwartz <wkschwartz@gmail.com>
    Will Hardy <django@willhardy.com.au>
    Wilson Miner <wminer@gmail.com>
    wojtek
+9 −4
Original line number Diff line number Diff line
@@ -411,7 +411,7 @@ class QuerySet(object):
        Inserts each of the instances into the database. This does *not* call
        save() on each of the instances, does not send any pre/post save
        signals, and does not set the primary key attribute if it is an
        autoincrement field.
        autoincrement field. Multi-table models are not supported.
        """
        # So this case is fun. When you bulk insert you don't get the primary
        # keys back (if it's an autoincrement), so you can't insert into the
@@ -423,13 +423,18 @@ class QuerySet(object):
        # this by using RETURNING clause for the insert query. We're punting
        # on these for now because they are relatively rare cases.
        assert batch_size is None or batch_size > 0
        if self.model._meta.parents:
            raise ValueError("Can't bulk create an inherited model")
        # Check that the parents share the same concrete model with the our
        # model to detect the inheritance pattern ConcreteGrandParent ->
        # MultiTableParent -> ProxyChild. Simply checking self.model._meta.proxy
        # would not identify that case as involving multiple tables.
        for parent in self.model._meta.get_parent_list():
            if parent._meta.concrete_model is not self.model._meta.concrete_model:
                raise ValueError("Can't bulk create a multi-table inherited model")
        if not objs:
            return objs
        self._for_write = True
        connection = connections[self.db]
        fields = self.model._meta.local_concrete_fields
        fields = self.model._meta.concrete_fields
        objs = list(objs)
        self._populate_pk_values(objs)
        with transaction.atomic(using=self.db, savepoint=False):
+4 −0
Original line number Diff line number Diff line
@@ -1768,6 +1768,10 @@ This has a number of caveats though:
  does not retrieve and set the primary key attribute, as ``save()`` does.
* It does not work with many-to-many relationships.

.. versionchanged:: 1.9

    Support for using ``bulk_create()`` with proxy models was added.

The ``batch_size`` parameter controls how many objects are created in single
query. The default is to create all objects in one batch, except for SQLite
where the default is such that at most 999 variables per query are used.
+3 −0
Original line number Diff line number Diff line
@@ -365,6 +365,9 @@ Management Commands
Models
^^^^^^

* :meth:`QuerySet.bulk_create() <django.db.models.query.QuerySet.bulk_create>`
  now works on proxy models.

* Database configuration gained a :setting:`TIME_ZONE <DATABASE-TIME_ZONE>`
  option for interacting with databases that store datetimes in local time and
  don't support time zones when :setting:`USE_TZ` is ``True``.
+19 −0
Original line number Diff line number Diff line
@@ -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)

Loading