Document Fields

New in version 0.23.


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 Doqu.

class doqu.ext.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.


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.

  • 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.ext.fields.FileField(base_path, **kwargs)

Handles externally stored files.


This field saves the file when process_outgoing() is triggered (see outgoing_processors in DocumentMetadata).

Outdated (replaced) files are not automatically removed.


class Doc(Document):
    attachment = FileField(base_path=MEDIA_ROOT+'attachments/')

d = Doc()
d.attachment = open('foo.txt')

dd = Doc.objects(db)[0]
Parameters:base_path – A string or callable: the directory where the files should be stored.

alias of FileWrapper

class doqu.ext.fields.ImageField(base_path, **kwargs)

A FileField that provides extended support for images. The ImageField.file is an ImageWrapper instance.


class Photo(Document):
    summary = Field(unicode)
    image = ImageField(base_path='photos/')

p = Photo(summary='Fido', image=open('fido.jpg'))

# playing with image
print "The photo is {0}×{1}px".format(*p.image.size)

alias of ImageWrapper

Project Versions

Previous topic

Document API

Next topic

Backend API

This Page