Code

Ticket #1496: model_wrapper_equality.diff

File model_wrapper_equality.diff, 669 bytes (added by akaihola, 8 years ago)

Enhanced equality test for models; works correctly for wrappers like UserWrapper

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 2509)
4+++ django/db/models/base.py    (working copy)
5@@ -77,1 +77,1 @@
6         return '<%s object>' % self.__class__.__name__
7
8     def __eq__(self, other):
9-        return isinstance(other, self.__class__) and self._get_pk_val() == other._get_pk_val()
10+        try:
11+            return (isinstance(other, self.__class__) or other._meta is self._meta) and self._get_pk_val() == other._get_pk_val()
12+        except AttributeError:
13+            return False
14
15     def __ne__(self, other):
16         return not self.__eq__(other)