heynemann / motorengine

Motorengine is a port of MongoEngine for Tornado.
http://motorengine.readthedocs.org
204 stars 64 forks source link

what is the `required=False` mean? #83

Closed Laisky closed 9 years ago

Laisky commented 9 years ago

If there is a users collection, someone has email and the others do not, how can I create a Users-document that can hold a document without the key email ?

class Users(Document):
    __collection__ = 'users'

    account = EmailField(required=True)
    name = EmailField(required=False)

user = Users(account='abc@gmail.com')
user.validate()

I want to create a document like that

{
   'account': 'abc@gmail.com'
}

But I got TypeError: expected string or buffer

BTW, There is another requirement that I want define a key that it's defalut value is anthor key's value,

as the uppon code, If I not pass the argument name, I want get a document like that

{
    'account': 'abc@gmail.com',
    'name': 'abc@gmail.com'
}

Is there any possible to do like that?

heynemann commented 9 years ago

The first one might be a bug on EmailField. Can you try it with a StringField to see if it works?

The second one you can do with defaults. Just use default=lambda item: item.name or item.account.

This is slightly different than what you want, though. It will save the value on mongo upon saving the document. There's no way to have a default value only when retrieving the document.

Hope this helps!

Laisky commented 9 years ago

Thanks.