-
Notifications
You must be signed in to change notification settings - Fork 132
@classmethod not working #81
Comments
By design, all Documents accessed via db ( class User(Document):
__collection__ = 'users'
structure = {
'id': int,
'screen_name': unicode,
'token': unicode,
'secret': unicode,
}
use_dot_notation = True
def create_or_update_by_id(self, id, screen_name, token, secret):
self.id = id
self.screen_name = screen_name
self.token = token
self.secret = secret
self.validate() # why not saving here ?
return self |
No, that doesn't work. The whole point is to create it only if the ID doesn't already exist. That's what the |
Well, in this case: class User(Document):
__collection__ = 'users'
structure = {
'id': int,
'screen_name': unicode,
'token': unicode,
'secret': unicode,
}
use_dot_notation = True
def create_or_update_by_id(self, id, screen_name, token, secret):
user = self.db.User.find_one({'_id': id}) or self.db.User()
user.id = id
user.screen_name = screen_name
user.token = token
user.secret = secret
user.validate() # why not saving here ?
return user |
That doesn't make any sense. Why would User have to specify itself in its methods? Or the db for that matter? And this is totally weird because self is an instance of User and you're using it to create new User objects. Completely un-Pythonic. Why don't class methods work? They are the only elegant solution. |
Yes, I know this is weird and completely un-Pythonic but this design inherits from a (very old) historical design. I tried to refactorise but the bad was already done (ie too many people were using MongoKit) and I don't have time anymore to fix this. If you want you can provide a patch to fix this issue. I re-open this ticket. |
I have the following code, with a class method.
But when I call it, with
db.User.create_or_update_by_id(...)
I get the following error:In other ORMs this works. Do class methods not work in MongoKit, or am I doing something wrong?
The text was updated successfully, but these errors were encountered: