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?

Answers


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

Need Your Help

Key Value in combobox lwuit?

java-me lwuit lwuit-list lwuit-combobox

I want to store and retrieve a key value associated with the comboBox. I have used only

check for length of characters in regex for amount validation

regex

I have a regex to validate the amount entered in a field: /^\d+(\.\d+)?$/

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.