开发者

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

I'm using the Python libraries for 开发者_如何学GoGoogle 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜