Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions django/core/serializers/xml_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def start_object(self, obj):
self.indent(self.indent_level)
attrs = {"model": str(obj._meta)}
if not self.use_natural_primary_keys or not self._resolve_natural_key(obj):
obj_pk = obj.pk
if obj_pk is not None:
if obj._is_pk_set():
attrs["pk"] = obj._meta.pk.value_to_string(obj)

try:
Expand Down
5 changes: 2 additions & 3 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,9 @@ def __eq__(self, other):
return NotImplemented
if self._meta.concrete_model != other._meta.concrete_model:
return False
my_pk = self.pk
if my_pk is None:
if not self._is_pk_set():
return self is other
return my_pk == other.pk
return self.pk == other.pk

def __hash__(self):
if not self._is_pk_set():
Expand Down
15 changes: 12 additions & 3 deletions docs/ref/models/instances.txt
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,18 @@ For example::

The equality method is defined such that instances with the same primary
key value and the same concrete class are considered equal, except that
instances with a primary key value of ``None`` aren't equal to anything except
themselves. For proxy models, concrete class is defined as the model's first
non-proxy parent; for all other models it's simply the model's class.
instances without a set primary key aren't equal to anything except
themselves. Whether the primary key is set is determined by
:meth:`~django.db.models.Model._is_pk_set`. For proxy models, concrete class
is defined as the model's first non-proxy parent; for all other models it's
simply the model's class.

.. versionchanged:: 6.2

Equality now uses :meth:`~django.db.models.Model._is_pk_set` instead of
comparing ``pk is None``. Unsaved instances with a composite primary key
or a ``db_default`` primary key are compared by identity, matching simple
``None`` primary keys.

For example::

Expand Down
6 changes: 6 additions & 0 deletions docs/releases/6.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ backends.
:meth:`~django.contrib.gis.gdal.Field.as_datetime` is now a ``c_float``
rather than a ``c_int``.

Models
------

* Unsaved instances with a composite primary key or a ``db_default`` primary
key no longer compare equal to other instances.

Tests
-----

Expand Down
4 changes: 4 additions & 0 deletions tests/basic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ def test_eq(self):
self.assertEqual(a, a)
self.assertEqual(a, mock.ANY)
self.assertNotEqual(Article(), a)
# DatabaseDefault is not None, but the pk is unset.
db_default = PrimaryKeyWithDbDefault()
self.assertEqual(db_default, db_default)
self.assertNotEqual(PrimaryKeyWithDbDefault(), db_default)

def test_hash(self):
# Value based on PK
Expand Down
17 changes: 17 additions & 0 deletions tests/composite_pk/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def test_pk_not_set_db_default(self):
self.assertIsNotNone(post.id)
self.assertIs(post._is_pk_set(), False)

def test_eq(self):
self.assertEqual(User(pk=(1, 2)), User(pk=(1, 2)))
self.assertEqual(User(tenant_id=2, id=3), User(tenant_id=2, id=3))
self.assertNotEqual(User(pk=(1, 2)), User(pk=(1, 3)))
self.assertNotEqual(User(pk=(1, 2)), object())
unset = User()
self.assertEqual(unset, unset)
self.assertNotEqual(User(), unset)
self.assertNotEqual(User(tenant_id=1), User(tenant_id=1))
self.assertNotEqual(User(id=1), User(id=1))
self.assertNotEqual(User(tenant_id=1), User(pk=(1, 2)))

def test_hash(self):
self.assertEqual(hash(User(pk=(1, 2))), hash((1, 2)))
self.assertEqual(hash(User(tenant_id=2, id=3)), hash((2, 3)))
Expand Down Expand Up @@ -425,6 +437,11 @@ def test_serialize_user_xml(self):
self.assertIn('<object model="composite_pk.user" pk=\'["1", "1"]\'>', result)
self.assert_deserializer(format="xml", users=users, serialized_users=result)

def test_serialize_unsaved_user_xml_omits_pk(self):
result = serializers.serialize("xml", [User()])
self.assertIn('<object model="composite_pk.user">', result)
self.assertNotIn(" pk=", result)

def test_serialize_post_uuid(self):
posts = Post.objects.filter(pk=(2, "11111111-1111-1111-1111-111111111111"))
result = serializers.serialize("json", posts)
Expand Down
Loading