Fields¶
The fields module contains a list of Field classes for model’s definition.
The example below shows the most common fields and builtin validations:
class Token(Model):
key = String()
secret = String()
class User(Model):
login = String(required=True)
name = String()
role = String(choices=['admin', 'moderator', 'user'])
email = Email(required=True)
token = Embedded(Token, required=True)
is_active = Boolean(default=False)
- class fields.Collection(model, *args, **kwargs)¶
Field subclass with builtin list of models.Model validation, encoding and decoding.
Example:
class Token(Model): key = String() secret = String() class User(Model): tokens = Collection(Token) user = User({ 'tokens': [ { 'key': 'xxx', 'secret': 'yyy' }, { 'key': 'zzz', 'secret': 'xxx' }, ] }) user.tokens.append(Token(key='yyy', secret='xxx'))
- class fields.Embedded(model, *args, **kwargs)¶
Field subclass with builtin embedded models.Model validation.
- class fields.Field(*validators, **kwargs)¶
This is the base class for all booby.fields. This class can also be used as field in any models.Model declaration.
Parameters: - default –
This field default‘s value.
If passed a callable object then uses its return value as the field’s default. This is particularly useful when working with mutable objects.
If default is a callable it can optionaly receive the owner model instance as its first positional argument.
- required – If True this field value should not be None.
- choices – A list of values where this field value should be in.
- name – Specify an alternate key name to use when decoding and encoding.
- read_only – If True, the value is treated normally in decoding but omitted during encoding.
- *validators – A list of field validators as positional arguments.
- default –