Methods and Document Class

44 views
Skip to first unread message

burli

unread,
Dec 5, 2011, 7:24:58 AM12/5/11
to couchdb...@googlegroups.com
Hi, how can I add methods/functions to a Document Class? For example

class User(Document):
    name = TextField()

    def is_active(self):
        return True

user = User(name = "Foo")

If I call user.is_active() I dont get "True". The result is

<bound method User.is_active of <User 'Foo' ....[snip]....>

Kxepal

unread,
Dec 5, 2011, 7:53:32 AM12/5/11
to couchdb...@googlegroups.com
Hi,

Everything seems to be working for me:

from couchdb.mapping import Document, TextField


class User(Document):
    name = TextField()

    def is_active(self):
        return True

user = User(name = "Foo")
assert user.is_active() is True


even if you have mistyped Document class:

from couchdb.mapping import TextField
from couchdb.client import Document


class User(Document):
    name = TextField()

    def is_active(self):
        return True

user = User(name = "Foo")
assert user.is_active() is True

All should works well. Could you provide standalone example that could reproduce problem?

--
,,,^..^,,,

burli

unread,
Dec 5, 2011, 8:06:36 AM12/5/11
to couchdb...@googlegroups.com
> from couchdb.client import Document
This is not documented. And if I add this I get the Error Message

AttributeError: 'User' object has no attribute 'store'

Alexander Shorin

unread,
Dec 5, 2011, 8:09:53 AM12/5/11
to couchdb...@googlegroups.com

This is the same as
from couchdb import Document

Just quite common error(: Anyway, what's about document method call problem?

--
,,,^..^,,,

burli

unread,
Dec 5, 2011, 8:17:54 AM12/5/11
to couchdb...@googlegroups.com
Here is the complete program

import couchdb
from couchdb.mapping import *

from uuid import uuid4


class User(Document):
    name = TextField()
    active = BooleanField(default=True)
    password = TextField()
    type = TextField(default="User")

    def is_active(self):
        return self.active
       
    def is_anonymous(self):
        return False
       
    def is_authenticated(self):
        return self.is_authenticated

   
couch = couchdb.Server()

try:
    db = couch.delete("mydb")
    db = couch.create("mydb")
except:
    db = couch.create("mydb")

u=User(name = u"Notch", password = u"ü")
u.id = u"Notch"
u.store(db)
u=User(name = u"Steve", password = u"b")
u.id = u"Steve"
u.store(db)
u=User(name = u"Creeper", active = False, password = u"c")
u.id = u"Creeper"
u.store(db)


user = User.load(db, "Notch")
print user.is_authenticated()

burli

unread,
Dec 5, 2011, 8:29:54 AM12/5/11
to couchdb...@googlegroups.com
Something went wrong with my last post. Here again

Here is the complete program

import couchdb
from couchdb.mapping import *

from uuid import uuid4

class User(Document):
    name = TextField()
If I add "from couchdb import Document" it does not work

burli

unread,
Dec 5, 2011, 8:44:22 AM12/5/11
to couchdb...@googlegroups.com
Looks like there is a different between

from couchdb.mapping import Document

and
from couchdb import Document

Only the Document Class from mapping supports store and load methods

Alexander Shorin

unread,
Dec 5, 2011, 2:58:39 PM12/5/11
to couchdb...@googlegroups.com
On Mon, Dec 5, 2011 at 5:29 PM, burli <m...@embedit.de> wrote:
> Something went wrong with my last post. Here again
>
> class User(Document):
>     name = TextField()
>     active = BooleanField(default=True)
>     password = TextField()
>     type = TextField(default="User")
>
>     def is_active(self):
>         return self.active
>
>     def is_anonymous(self):
>         return False
>
>     def is_authenticated(self):
>         return self.is_authenticated
>
> ...

>
> user = User.load(db, "Notch")
> print user.is_authenticated()
>

Take a look at User.is_authenticated method - it just returns itself(;
Other methods works as expected.


--
,,,^..^,,,

burli

unread,
Dec 6, 2011, 2:57:13 AM12/6/11
to couchdb...@googlegroups.com

Take a look at User.is_authenticated method - it just returns itself(;
Other methods works as expected.

Sorry, copy & paste failure. Should just return True.

I fixed the problem now. There where some other bugs. But I found that couchdb.mapping Document is not the same as couchdb Document.

Thanks
Markus

Alexander Shorin

unread,
Dec 6, 2011, 5:36:30 AM12/6/11
to couchdb...@googlegroups.com

Yes, couchdb.Document and couchdb.mapping.Document are different.
First one if dict based class that represents raw CouchDB document as
it is, while second one tries to objectify it and give the warranty
that document would always have some fields with certain type.

--
,,,^..^,,,

Reply all
Reply to author
Forward
0 new messages