Table Of Contents

Previous topic

Common Exceptions

Next topic

Primitives

This Page

Models

The spyne.model package contains the Spyne type markers to denote primitive and complex types in object and method definitions.

Spyne has built-in support for most common data types and provides an API for those who’d like to implement their own.

There are five types of models in Spyne:

Base Classes

class spyne.model.ModelBase

Bases: object

The base class for type markers. It defines the model interface for the interface generators to use and also manages class customizations that are mainly used for defining constraints on input values.

__orig__ = None

This holds the original class the class .customize()d from. Ie if this is None, the class is not a customize()d one.

__extends__ = None

This holds the original class the class inherited or .customize()d from. This is different from __orig__ because it’s only set when cls.is_default(cls) == False

__namespace__ = None

The public namespace of this class. Use get_namespace() instead of accessing it directly.

__type_name__ = None

The public type name of the class. Use get_type_name() instead of accessing it directly.

class Annotations

Bases: object

The class that holds the annotations for the given type.

appinfo = None

Any object that carries app-specific info.

doc = ''

The public documentation for the given type.

class ModelBase.Attributes

Bases: object

The class that holds the constraints for the given type.

db_type = None

When not None, it overrides Spyne’s own mapping from Spyne types to SQLAlchemy types. It’s a standard SQLAlchemy type marker, e.g. sqlalchemy.Integer.

default = None

The default value if the input is None

default_factory = None

The default value if the input is None

empty_is_none = False

When the incoming object is empty (e.g. ‘’ for strings) treat it as None. No effect (yet) for outgoing values.

exc_interface = False

If true, this field will be excluded from the interface document.

exc_mapper = False

If true, this field will be excluded from the table mapper of the parent class.

exc_table = False

If true, this field will be excluded from the table of the parent class.

index = None

Can be True, a string, or a tuple of two strings.

  • If True, this object will be set as indexed in the database schema with default options.
  • If the value is a string, the value will denote the indexing method used by the database. See: http://www.postgresql.org/docs/9.2/static/indexes-types.html
  • If the vale is a tuple of two strings, the first value will denote the index name and the second value will denote the indexing method as above.
logged = True

If false, this object will be ignored in log_repr, mostly used for logging purposes.

max_occurs = 1

Can be set to any strictly positive integer. Values greater than 1 will imply an iterable of objects as native python type. Can be set to decimal.Decimal("inf") for arbitrary number of arguments.

min_occurs = 0

Set this to 1 to make this object mandatory. Can be set to any positive integer. Note that an object can still be null or empty, even if it’s there.

nillable = True

Set this to false to reject null values. Synonyms with nullable. True by default. The default value can be changed by

setting AttributesMeta.NULLABLE_DEFAULT.
order = None

An integer that’s passed to _type_info.insert() as first argument when not None. .append() is used otherwise.

prot_attrs = None

to be implemented.

read_only = False

If True, the attribute won’t be initialized from outside values.

schema_tag = '{http://www.w3.org/2001/XMLSchema}element'

The tag used to add a primitives as child to a complex type in the xml schema.

sqla_column_args = None

A dict that will be passed to SQLAlchemy’s Column constructor as **kwargs.

sub_name = None

This specifies which string should be used as field name when this type is seriazed under a ComplexModel.

sub_ns = None

An Xml-specific attribute that specifies which namespace should be used for field names in classes.

table_name = None

Database table name.

translations = None

A dict that contains locale codes as keys and translations of field names to that language as values.

unique = None

If True, this object will be set as unique in the database schema with default indexing options. If the value is a string, it will be used as the indexing method to create the unique index. See sqlalchemy documentation on how to create multi-column unique constraints.

xml_choice_group = None

When not None, shares the same <choice> tag with fields with the same xml_choice_group value.

classmethod ModelBase.customize(**kwargs)

Duplicates cls and overwrites the values in cls.Attributes with **kwargs and returns the new class.

classmethod ModelBase.get_namespace()

Returns the namespace of the class. Defaults to the python module name.

classmethod ModelBase.get_namespace_prefix(interface)

Returns the namespace prefix for the given interface. The get_namespace_prefix of the interface class generates a prefix if none is defined.

classmethod ModelBase.get_type_name()

Returns the class name unless the __type_name__ attribute is defined.

classmethod ModelBase.get_type_name_ns(interface)

Returns the type name with a namespace prefix, separated by a column.

static ModelBase.resolve_namespace(default_ns, tags=None)

This call finalizes the namespace assignment. The default namespace is not available until the application calls populate_interface method of the interface generator.

classmethod ModelBase.to_string(value)

Returns str(value). This should be overridden if this is not enough.

static ModelBase.validate_native(value)

Override this method to do your own input validation on the native value. This is called after converting the incoming string to the native python value.

static ModelBase.validate_string(value)

Override this method to do your own input validation on the input string. This is called before converting the incoming string to the native python value.

class spyne.model.SimpleModel

Bases: spyne.model._base.ModelBase

The base class for primitives.

class Attributes

Bases: spyne.model._base.Attributes

The class that holds the constraints for the given type.

values = set([])

The set of possible values for this type.

static SimpleModel.__new__(**kwargs)

Overriden so that any attempt to instantiate a primitive will return a customized class instead of an instance.

See spyne.model.base.ModelBase for more information.

classmethod SimpleModel.customize(**kwargs)

Duplicates cls and overwrites the values in cls.Attributes with **kwargs and returns the new class.