Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override equals() in google app engine data model type?

I'm using the Python libraries for Google App Engine. How can I override the equals() method on a class so that it judges equality on the user_id field of the following class:

class UserAccount(db.Model):
    # compare all equality tests on user_id
    user = db.UserProperty(required=True)
    user_id = db.StringProperty(required=True)
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    notifications = db.ListProperty(db.Key)

Right now, I'm doing equalty by getting a UserAccount object and doing user1.user_id == user2.user_id. Is there a way I can override it so that 'user1 == user2' will look at only the 'user_id' fields?

Thanks in advance

like image 623
Cuga Avatar asked Jun 19 '10 03:06

Cuga


1 Answers

Override operators __eq__ (==) and __ne__ (!=)

e.g.

class UserAccount(db.Model):

    def __eq__(self, other):
        if isinstance(other, UserAccount):
            return self.user_id == other.user_id
        return NotImplemented

    def __ne__(self, other):
        result = self.__eq__(other)
        if result is NotImplemented:
            return result
        return not result
like image 152
Anurag Uniyal Avatar answered Oct 26 '22 03:10

Anurag Uniyal