Commit 8adc5903 authored by Anssi Kääriäinen's avatar Anssi Kääriäinen Committed by Tim Graham
Browse files

Fixed #23617 -- Added get_pk_value_on_save()

The method is mainly intended for use with UUIDField. For UUIDField we
want to call the field's default even when primary key value is
explicitly set to None to match the behavior of AutoField.

Thanks to Marc Tamlyn and Tim Graham for review.
parent 118b1122
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -771,6 +771,9 @@ class Model(six.with_metaclass(ModelBase)):
                       if f.name in update_fields or f.attname in update_fields]

        pk_val = self._get_pk_val(meta)
        if pk_val is None:
            pk_val = meta.pk.get_pk_value_on_save(self)
            setattr(self, meta.pk.attname, pk_val)
        pk_set = pk_val is not None
        if not pk_set and (force_update or update_fields):
            raise ValueError("Cannot force an update in save() with no primary key.")
+11 −0
Original line number Diff line number Diff line
@@ -503,6 +503,17 @@ class Field(RegisterLookupMixin):
        return _load_field, (self.model._meta.app_label, self.model._meta.object_name,
                             self.name)

    def get_pk_value_on_save(self, instance):
        """
        Hook to generate new PK values on save. This method is called when
        saving instances with no primary key value set. If this method returns
        something else than None, then the returned value is used when saving
        the new instance.
        """
        if self.default:
            return self.get_default()
        return None

    def to_python(self, value):
        """
        Converts the input value into the expected Python data type, raising
+7 −0
Original line number Diff line number Diff line
@@ -398,6 +398,11 @@ class QuerySet(object):
        obj.save(force_insert=True, using=self.db)
        return obj

    def _populate_pk_values(self, objs):
        for obj in objs:
            if obj.pk is None:
                obj.pk = obj._meta.pk.get_pk_value_on_save(obj)

    def bulk_create(self, objs, batch_size=None):
        """
        Inserts each of the instances into the database. This does *not* call
@@ -422,6 +427,8 @@ class QuerySet(object):
        self._for_write = True
        connection = connections[self.db]
        fields = self.model._meta.local_concrete_fields
        objs = list(objs)
        self._populate_pk_values(objs)
        with transaction.atomic(using=self.db, savepoint=False):
            if (connection.features.can_combine_inserts_with_and_without_auto_increment_pk
                    and self.model._meta.has_auto_field):
+9 −0
Original line number Diff line number Diff line
@@ -217,6 +217,15 @@ Note that ``lambda``\s cannot be used for field options like ``default``
because they cannot be :ref:`serialized by migrations <migration-serializing>`.
See that documentation for other caveats.

The default value is used when new model instances are created and a value
isn't provided for the field. When the field is a primary key, the default is
also used when the field is set to ``None``.

.. versionchanged:: 1.8

    The default wasn't used for ``None`` primary key values in previous
    versions.

``editable``
------------

+3 −0
Original line number Diff line number Diff line
@@ -526,6 +526,9 @@ Models
* You can now get the set of deferred fields for a model using
  :meth:`Model.get_deferred_fields() <django.db.models.Model.get_deferred_fields>`.

* Model field ``default``’s are now used when primary key field's are set to
  ``None``.

Signals
^^^^^^^

Loading