Document Fields

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.

class doqu.fields.Field(datatype, essential=False, required=False, default=None, choices=None, label=None, pickled=False)

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:
  • essential – if True, validator Exists is added (i.e. the field may be empty but it must be present in the record).
  • pickled – if True, the value is preprocessed with pickle’s dumps/loads functions. This of course breaks lookups by this field but enables storing arbitrary Python objects.
class doqu.fields.FileField(base_path, **kwargs)

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()

Previous topic

Document API

Next topic

Backend API

This Page