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

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.ext.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(base_path=MEDIA_ROOT+'attachments/')

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

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

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.

Usage:

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

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

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

alias of ImageWrapper

Project Versions

Previous topic

Document API

Next topic

Backend API

This Page