New in version 0.23.
Note
This abstraction is by no means a complete replacement for the normal approach of semantic grouping. Please use it with care. Also note that the API can change. The class can even be removed in future versions of Docu.
Representation of a document property. Syntax sugar for separate definitions of structure, validators, defaults and labels.
Usage:
class Book(Document):
title = Field(unicode, required=True, default=u'Hi', label='Title')
this is just another way to type:
class Book(Document):
structure = {
'title': unicode
}
validators = {
'title': [validators.Required()]
}
defaults = {
'title': u'Hi'
}
labels = {
'title': u'The Title'
}
Nice, eh? But be careful: the title definition in the first example barely fits its line. Multiple long definitions will turn your document class into an opaque mess of characters, while the semantically grouped definitions stay short and keep related things aligned together. “Semantic sugar” is sometimes pretty bitter, use it with care.
Complex validators still need to be specified by hand in the relevant dictionary. This can be worked around by creating specialized field classes (e.g. EmailField) as it is done e.g. in Django.
Parameters: |
|
---|
Handles externally stored files.
Warning
This field saves the file when process_outgoing() is triggered (see outgoing_processors in DocumentMetadata).
Outdated (replaced) files are not automatically removed.
Usage:
class Doc(Document):
attachment = FileField()
d = Doc()
d.attachment = open('foo.txt')
d.save(db)
dd = Doc.objects(db)[0]
print dd.attachment.file.read()