From 6af05e7a0f0e4604d6a67899acaa99d73ec0dfaa Mon Sep 17 00:00:00 2001 From: Anssi Kääriäinen Date: Wed, 14 Aug 2013 11:05:01 +0300 Subject: Fixed model.__eq__ and __hash__ for no pk value cases The __eq__ method now considers two instances without primary key value equal only when they have same id(). The __hash__ method raises TypeError for no primary key case. Fixed #18864, fixed #18250 Thanks to Tim Graham for docs review. --- docs/ref/models/instances.txt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'docs/ref') diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index 6295eb0407..da657a9a01 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -521,6 +521,25 @@ For example:: In previous versions only instances of the exact same class and same primary key value were considered equal. +``__hash__`` +------------ + +.. method:: Model.__hash__() + +The ``__hash__`` method is based on the instance's primary key value. It +is effectively hash(obj.pk). If the instance doesn't have a primary key +value then a ``TypeError`` will be raised (otherwise the ``__hash__`` +method would return different values before and after the instance is +saved, but changing the ``__hash__`` value of an instance `is forbidden +in Python`_). + +.. versionchanged:: 1.7 + + In previous versions instance's without primary key value were + hashable. + +.. _is forbidden in Python: http://docs.python.org/reference/datamodel.html#object.__hash__ + ``get_absolute_url`` -------------------- -- cgit v1.3