Why doesn't python regard two discrete instances of a django model to be the same object?
(Using django as an example; the ForeignKey is implicit in the example)
>>> from coconuts.models import Coconut, Swallow >>> c1 = Coconut.objects.get(id=1) >>> s = Swallow.objects.get(id=1) >>> c2 = s.coconuts_carried.filter(id=1) >>> c1 == c2 True >>> c1 is c2 False
My understanding is that 'is' tests identity, while == tests equality. In this case, c1 and c2 are the same exact object. Why "isn't" they the same thing?
is compares python objects, not objects in your database. each query returns a new python objects, and so c and c2 are different.
to see this, try changing some of the properties of c or c2. Only one of them will change.
as can be seen from the docs,
c == c2
is equivalent to
c.id == c2.id