I am trying to emulate django models :
In Django :
from django.contrib.auth.models import User
if User.objects.get(pk=1) == User.objects.get(username='root')
print 'True'
else:
print 'False'
# True is printed
My implementation :
class MyUser:
def __init__(self, id):
self.id = id
class User:
class objects:
@staticmethod
def get(**kwargs):
return MyUser(1)
if User.objects.get(pk=1) == User.objects.get(username='root')
print 'True'
else:
print 'False'
# False is printed
How to correct my implementation to get ‘True’ ?
How can I achieve the same effect ?
What change shall I do ?
The issue is simply that you haven’t defined
__eq__on your class, so Python has no idea how to compare them. Something like this would work: