flywheel.fields package

Module contents

Field declarations for models

class flywheel.fields.Composite(*args, **kwargs)[source]

Bases: flywheel.fields.Field

A field that is composed of multiple other fields

Parameters:

*fields : list

List of names of fields that compose this composite field

hash_key : bool, optional

This key is a DynamoDB hash key (default False)

range_key : bool, optional

This key is a DynamoDB range key (default False)

index : str, optional

If present, create a local secondary index on this field with this as the name.

data_type : str, optional

The dynamo data type. Valid values are (NUMBER, STRING, BINARY, NUMBER_SET, STRING_SET, BINARY_SET, dict, list, bool, str, unicode, int, float, set, datetime, date, Decimal) (default unicode)

coerce : bool, optional

Attempt to coerce the value if it’s the incorrect type (default False)

check : callable, optional

A function that takes the value and returns True if the value is valid (default None)

merge : callable, optional

The function that merges the subfields together. By default it simply joins them with a ‘:’.

get_cached_value(obj)[source]
resolve(obj=None, scope=None)[source]

Resolve a field value from an object or scope dict

class flywheel.fields.Field(hash_key=False, range_key=False, index=None, data_type=<type 'unicode'>, coerce=False, check=None, nullable=True, default=<object object>)[source]

Bases: object

Declarative way to specify model fields

Parameters:

hash_key : bool, optional

This key is a DynamoDB hash key (default False)

range_key : bool, optional

This key is a DynamoDB range key (default False)

index : str, optional

If present, create a local secondary index on this field with this as the name.

data_type : object, optional

The field data type. You may use int, unicode, set, etc. or you may pass in an instance of TypeDefinition (default unicode)

coerce : bool, optional

Attempt to coerce the value if it’s the incorrect type (default False)

check : callable or list, optional

A function that takes the value and returns True if the value is valid. May also be a list of such functions. (default None)

nullable : bool, optional

If false, will add a check (above) to ensure the value is not null (default True).

default : object, optional

The default value for this field that will be set when creating a model (default None, except for set data types which default to set())

Notes

Field(index='my-index')

Is shorthand for:

Field().all_index('my-index')

Attributes

name (str) The name of the attribute on the model
model (class) The Model this field is attached to
composite (bool) True if this is a composite field
all_index(name)[source]

Index this field and project all attributes

Parameters:

name : str

The name of the index

beginswith_(other)[source]

Create a query condition that this field must begin with a string

between_(low, high)[source]

Create a query condition that this field must be between two values (inclusive)

betwixt_(low, high)[source]

Poetic version of between_()

can_resolve(fields)[source]

Check if the provided fields are enough to fully resolve this field

Parameters:

fields : list or set

Returns:

needed : set

Set of the subfields needed to resolve this field. If empty, then it cannot be resolved.

coerce(value, force_coerce=None)[source]

Coerce the value to the field’s data type

contains_(other)[source]

Create a query condition that this field must contain a value

ddb_data_type[source]

Get the native DynamoDB data type

ddb_dump(value)[source]

Dump a value to its Dynamo format

ddb_dump_for_query(value)[source]

Dump a value to format for use in a Dynamo query

classmethod ddb_dump_overflow(val)[source]

Dump an overflow value to its Dynamo format

ddb_load(val)[source]

Decode a value retrieved from Dynamo

classmethod ddb_load_overflow(val)[source]

Decode a value of an overflow field

default[source]

Get a shallow copy of the default value

get_cached_value(obj)[source]

Get the cached value of a field before any local modifications

get_ddb_index()[source]

Construct a dynamo local index object

in_(other)[source]

Create a query condition that this field must be within a set of values

include_index(name, includes=None)[source]

Index this field and project selected attributes

Parameters:

name : str

The name of the index

includes : list, optional

List of non-key attributes to project into this index

is_mutable[source]

Return True if the data type is mutable

classmethod is_overflow_mutable(val)[source]

Check if an overflow field is mutable

is_set[source]

Return True if data type is a set

keys_index(name)[source]

Index this field and project all key attributes

Parameters:

name : str

The name of the index

ncontains_(other)[source]

Create a query condition that this field cannot contain a value

resolve(obj=None, scope=None)[source]

Resolve a field value from an object or scope dict

validate(obj)[source]

Run the validation checks for this field on a model object.

Parameters:

obj : Model

Raises:

err : ValueError

Raised if any of the checks fail.