Ticket #14492: model_eq_14405.diff

File model_eq_14405.diff, 1.8 KB (added by Byron Ruth, 14 years ago)
  • django/db/models/base.py

     
    370370        return '%s object' % self.__class__.__name__
    371371
    372372    def __eq__(self, other):
    373         return isinstance(other, self.__class__) and self._get_pk_val() == other._get_pk_val()
     373        if isinstance(other, self.__class__):
     374            return self._get_pk_val() == other._get_pk_val()
    374375
     376        # ensure we are dealing with another Model instance
     377        if not isinstance(other, Model):
     378            return
     379
     380        # self is proxy, other is not
     381        proxy = self._meta.proxy_for_model
     382        if self._meta.proxy is True and isinstance(other, proxy):
     383            return self._get_pk_val() == other._get_pk_val()
     384
     385        # other is proxy, self is not
     386        proxy = other._meta.proxy_for_model
     387        if other._meta.proxy is True and isinstance(self, proxy):
     388            return self._get_pk_val() == other._get_pk_val()
     389        return False
     390
    375391    def __ne__(self, other):
    376392        return not self.__eq__(other)
    377393
  • tests/modeltests/proxy_models/tests.py

     
    312312        management.call_command('loaddata', 'mypeople.json', verbosity=0, commit=False)
    313313        p = MyPerson.objects.get(pk=100)
    314314        self.assertEqual(p.name, 'Elvis Presley')
     315
     316    def test_eq(self):
     317        s = State(pk=3)
     318        sp = StateProxy(pk=3)
     319        self.assertEqual(s, sp)
     320        self.assertEqual(sp, s)
     321        sp = StateProxy(pk=4)
     322        self.assertNotEqual(s, sp)
     323        self.assertNotEqual(sp, s)
Back to Top