Fixed #2936, #6500 -- Added a __hash__() method to Models (since we implement our own __eq__ method).

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7132 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-02-19 01:59:34 +00:00
parent f1a24be01c
commit 343fa35a2c
2 changed files with 14 additions and 0 deletions

View File

@ -135,6 +135,9 @@ class Model(object):
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
def __hash__(self):
return hash(self._get_pk_val())
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs) dispatcher.send(signal=signals.pre_init, sender=self.__class__, args=args, kwargs=kwargs)

View File

@ -5,6 +5,11 @@
This is a basic model with only two non-primary-key fields. This is a basic model with only two non-primary-key fields.
""" """
try:
set
except NameError:
from sets import Set as set
from django.db import models from django.db import models
class Article(models.Model): class Article(models.Model):
@ -389,4 +394,10 @@ year, including Jan. 1 and Dec. 31.
>>> a.save() >>> a.save()
>>> Article.objects.get(pk=a.id).headline >>> Article.objects.get(pk=a.id).headline
u'\u6797\u539f \u3081\u3050\u307f' u'\u6797\u539f \u3081\u3050\u307f'
# Model instances have a hash function, so they can be used in sets or as
# dictionary keys. Two models compare as equal if their primary keys are equal.
>>> s = set([a10, a11, a12])
>>> Article.objects.get(headline='Article 11') in s
True
""" """