Field reference

This section contains all the details of the resource fields built into Odin.

Field options

The following arguments are available to all field types. All are optional.

verbose_name

Field.verbose_name

A human-readable name for the field. If the verbose name isn’t given, Odin will automatically create it using the field’s attribute name, converting underscores to spaces.

verbose_name_plural

Field.verbose_name_plural

A human-readable name for the field. If the verbose name isn’t given, Odin will automatically create it using the field’s attribute name, converting underscores to spaces.

name

Field.name

Name of the field as it appears in the exported document. If the name isn’t given, Odin will use the field’s attribute name.

null

Field.null

If True Odin will raise a validation error if a value is null. Default is False.

choices

Field.choices

An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field. If this is given, the choices are used to validate entries.

Note

Choices are also used by the odin.contrib.doc_gen to generate documentation.

A choices list looks like this:

GENRE_CHOICES = (
    ('sci-fi', 'Science Fiction'),
    ('fantasy', 'Fantasy'),
    ('others', 'Others'),
)

The first element in each tuple is the value that will be used to validate, the second element is used for documentation. For example:

import Odin

class Book(Odin.Resource):
    GENRE_CHOICES = (
        ('sci-fi', 'Science Fiction'),
        ('fantasy', 'Fantasy'),
        ('others', 'Others'),
    )
    title = Odin.StringField()
    genre = Odin.StringField(choices=GENRE_CHOICES)
>>> b = Book(title="Consider Phlebas", genre="sci-fi")
>>> b.genre
'sci-fi'

default

Field.default

The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.

doc_text (help_text)

Field.doc_text

Doc text is used by the odin.contrib.doc_gen to generate documentation.

Note

help_text will be deprecated in a future release in favor of doc_text.

Also useful for inline documentation even if documentation is not generated.

validators

Field.validators

error_messages

Field.error_messages

is_attribute

Field.is_attribute

use_default_if_not_provided

Field.use_default_if_not_provided

Standard fields

Simple field types.

StringField

class StringField([max_length=None, **options])

A string.

StringField has one extra argument:

StringField.max_length
The maximum length (in characters) of the field. The max_length value is enforced Odin’s validation.

IntegerField

class IntegerField([min_value=None, max_value=None, **options])

An integer.

IntegerField has two extra arguments:

IntegerField.min_value
The minimum value of the field. The min_value value is enforced Odin’s validation.
IntegerField.max_value
The maximum value of the field. The max_value value is enforced Odin’s validation.

FloatField

class FloatField([**options])

A floating-point number represented in Python by a float instance.

FloatField has two extra arguments:

FloatField.min_value
The minimum value of the field. The min_value value is enforced Odin’s validation.
FloatField.max_value
The maximum value of the field. The max_value value is enforced Odin’s validation.

BooleanField

class BooleanField([**options])

A true/false field.

DateField

class DateField([**options])

A date field or date encoded in ISO-8601 date string format.

TimeField

class TimeField([assume_local=True, **options])

A time field or time encoded in ISO-8601 time string format.

TimeField has an extra argument:

TimeField.assume_local
This adjusts the behaviour of how a naive time (time objects with no timezone) or time strings with no timezone specified. By default assume_local is True, in this state naive time objects are assumed to be in the current system timezone. Similarly on decoding a time string the output time will be converted to the current system timezone.

DateTimeField

class DateTimeField([**options])

A datetime field or date encoded in ISO-8601 datetime string format.

DateTimeField has an extra argument:

DateTimeField.assume_local
This adjusts the behaviour of how a naive time (date time objects with no timezone) or date time strings with no timezone specified. By default assume_local is True, in this state naive datetime objects are assumed to be in the current system timezone. Similarly on decoding a date time string the output datetime will be converted to the current system timezone.

HttpDateTimeField

class HttpDateTimeField([**options])

A datetime field or date encoded in ISO-1123 or HTTP datetime string format.

ArrayField

class ArrayField([**options])

An array structure represented in Python by a list instance.

TypedArrayField

class TypedArrayField(field, [**options])

An array structure represented in Python by a list instance accepts an additional parameter of another field type that each entry in the array is validated against.

TypedArrayField.field
An instance of an odin field that is used to validate each entry in the array.

DictField

class DictField([**options])

A dictionary.

Note

The object values in the object are not defined.

Composite fields

Odin also defines a set of fields that allow for composition.

DictAs field

class DictAs(of[, **options])

A child object. Requires a positional argument: the class that represents the child resource.

Note

A default dict is automatically assigned.

ArrayOf field

class ArrayOf(of[, **options])

A child list. Requires a positional argument: the class that represents a list of resources.

Note

A default list is automatically assigned.

DictOf field

class DictOf(of[, **options])

A child dict. Requires a positional argument: the class that represents a dict (or hash map) of resources.

Note

A default dict is automatically assigned.

Virtual fields

Virtual fields are special fields that can be used to calculate a value or provide a value lookup. Unlike using a property a virtual field is also a treating like field in that it can be mapped or exported.

Note

You can use the

Virtual fields share many of the options of regular fields:

Calculated field

class CalculatedField(expr[, **options])