Ticket #17851: fix-db-field-compare-with-advanced-comparison.diff

File fix-db-field-compare-with-advanced-comparison.diff, 2.2 KB (added by Simon Charette, 13 years ago)
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 121797a..1f87a1b 100644
    a b def __init__(self, verbose_name=None, name=None, primary_key=False,  
    123123        messages.update(error_messages or {})
    124124        self.error_messages = messages
    125125
    126     def __cmp__(self, other):
     126    def __lt__(self, other):
    127127        # This is needed because bisect does not take a comparison function.
    128         return cmp(self.creation_counter, other.creation_counter)
     128        if isinstance(other, Field):
     129            return self.creation_counter < other.creation_counter
     130        return NotImplemented
    129131
    130132    def __deepcopy__(self, memodict):
    131133        # We don't have to deepcopy very much here, since most things are not
  • tests/modeltests/basic/tests.py

    diff --git a/tests/modeltests/basic/tests.py b/tests/modeltests/basic/tests.py
    index f9141dc..29418e4 100644
    a b  
    33from datetime import datetime
    44
    55from django.core.exceptions import ObjectDoesNotExist
    6 from django.db.models.fields import FieldDoesNotExist
     6from django.db.models.fields import Field, FieldDoesNotExist
    77from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
    88from django.utils.translation import ugettext_lazy
    99
    def test_hash_function(self):  
    509509
    510510        s = set([a10, a11, a12])
    511511        self.assertTrue(Article.objects.get(headline='Article 11') in s)
     512       
     513    def test_field_comparison(self):
     514        # Field instances have a cmp function to allow the ORM to determine
     515        # in which order fields are defined on a model. Plus, field should
     516        # be comparable to non Field objects also. Make sure it works correctly
     517        # since it overrides __cmp__ method.
     518        f1 = Field()
     519        f2 = Field(auto_created=True)
     520        f3 = Field()
     521        self.assertTrue(f2 < f1)
     522        self.assertTrue(f1 < f3)
     523        self.assertFalse(f1 == None)
     524        self.assertFalse(f2 in (None, 1, ''))
    512525
    513526    def test_extra_method_select_argument_with_dashes_and_values(self):
    514527        # The 'select' argument to extra() supports names with dashes in
Back to Top