Django

Code

Changeset 7132

Show
Ignore:
Timestamp:
02/18/08 19:59:34 (5 months ago)
Author:
mtredinnick
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/base.py

    r7116 r7132  
    135135    def __ne__(self, other): 
    136136        return not self.__eq__(other) 
     137 
     138    def __hash__(self): 
     139        return hash(self._get_pk_val()) 
    137140 
    138141    def __init__(self, *args, **kwargs): 
  • django/trunk/tests/modeltests/basic/models.py

    r6453 r7132  
    55This is a basic model with only two non-primary-key fields. 
    66""" 
     7 
     8try: 
     9    set 
     10except NameError: 
     11    from sets import Set as set 
    712 
    813from django.db import models 
     
    390395>>> Article.objects.get(pk=a.id).headline 
    391396u'\u6797\u539f \u3081\u3050\u307f' 
     397 
     398# Model instances have a hash function, so they can be used in sets or as 
     399# dictionary keys. Two models compare as equal if their primary keys are equal. 
     400>>> s = set([a10, a11, a12]) 
     401>>> Article.objects.get(headline='Article 11') in s 
     402True 
    392403"""