Base fields

Base classes for build database fields.

class yadm.fields.base.NotLoadedError

Raise if value marked as not loaded.

doc = db(Doc).fields('a').find_one()
try:
    doc.b
except NotLoadedError:
    print("raised!")
class yadm.fields.base.FieldDescriptor(name, field)

Base desctiptor for fields.

name

Name of field

field

Field instance for this desctiptor

__delete__(instance)

Mark document’s key as not set.

__get__(instance, owner)

Get python value from document.

  1. Lookup in __changed__;

  2. Lookup in __cache__;

  3. Lookup in __raw__:

    • if AttributeNotSet – call Field.get_if_attribute_not_set;
    • if NotLoaded – call Field.get_if_not_loaded;
    • call Field.from_mongo;
    • set __name__ and __parent__
    • save to __cache__
  4. Call Field.get_default;

  5. If AttributeNotSet – call Field.get_if_attribute_not_set;

  6. Return value.

__set__(instance, value)

Set value to document.

  1. Call Field.prepare_value for cast value;
  2. Save in Document.__changed__;
  3. Call Field.set_parent_changed.
class yadm.fields.base.Field(smart_null=False)

Base field for all database fields.

Parameters:smart_null (bool) – If it True, access to not exists fields return None instead AttributeError exception. You will not be able to distinguish null value from not exist. Use with care.
descriptor_class

Class of desctiptor for work with field

document_class

Class of document. Set in contribute_to_class().

name

Name of field in document. Set in contribute_to_class().

contribute_to_class(document_class, name)

Add field for document_class.

Parameters:document_class (MetaDocument) – document class for add
copy()

Return copy of field.

descriptor_class

alias of FieldDescriptor

from_mongo(document, value)

Convert mongo value to python value.

Parameters:
  • document (BaseDocument) – document
  • value – mongo value
Returns:

python value

get_default(document)

Return default value.

get_fake(document, faker, deep)

Return fake data for testing.

get_if_attribute_not_set(document)

Call if key not exist in document.

get_if_not_loaded(document)

Call if field data marked as not loaded.

prepare_value(document, value)

The method is called when value is assigned for the attribute.

Parameters:
Returns:

prepared value

It must be accept value argument and return processed (e.g. casted) analog. Also it is called once for the default value.

to_mongo(document, value)

Convert python value to mongo value.

Parameters:
  • document (BaseDocument) – document
  • value – python value
Returns:

mongo value