Resources

The BaseResource class

class flask_restalchemy.resources.BaseResource(request_decorators=None)[source]

Bases: flask.views.MethodView

The Base class for resources

Parameters:request_decorators (dict|list) – a list of decorators
dispatch_request(*args, **kwargs)[source]

Subclasses have to override this method to implement the actual view function code. This method is called with all the arguments from the URL rule.

The BaseModelResource class

class flask_restalchemy.resources.BaseModelResource(declarative_model, serializer, session_getter, query_modifier=None, request_decorators=None)[source]

Bases: flask_restalchemy.resources.resources.BaseResource

The Base class for ORM resources

Parameters:
  • declarative_model (class) – the SQLAlchemy declarative class.
  • serializer (ModelSerializer) – schema for serialization. If None, a default serializer will be created.
  • session_getter (callable) – a callable that returns the DB session. A callable is used since a reference to DB session may not be available on the resource initialization.
  • query_modifier (callable) – function that returns a query and expects a model as parameter that should be used to create the query and expects a parent_query to be incremented with the callback query function. The method signature should look like this: query_callback(parent_query, resource_model)
  • request_decorators (dict|list) – a list of decorators

The ModelResource class

class flask_restalchemy.resources.ModelResource(declarative_model, serializer, session_getter, query_modifier=None, request_decorators=None)[source]

Bases: flask_restalchemy.resources.resources.BaseModelResource

delete(id)[source]
get(id=None)[source]
methods = {'DELETE', 'GET', 'POST', 'PUT'}
post()[source]
put(id)[source]

The ToManyRelationResource class

class flask_restalchemy.resources.ToManyRelationResource(relation_property, serializer, session_getter, query_modifier=None, request_decorators=None)[source]

Bases: flask_restalchemy.resources.resources.BaseModelResource

Resource class that receives an SQLAlchemy relationship define the API to provide LIST and CREATE over data of the child model associated with a specific element of the parent model.

Parameters:
  • relation_property (relationship) – the SQLAlchemy relationship.
  • serializer (ModelSerializer) – schema for serialization. If None, a default serializer will be created.
  • session_getter (callable) – a callable that returns the DB session. A callable is used since a reference to DB session may not be available on the resource initialization.
  • query_modifier (callable) – function that returns a query and expects a model as parameter that should be used to create the query and expects a parent_query to be incremented with the callback query function. The method signature should look like this: query_callback(parent_query, resource_model)
  • request_decorators (dict|list) – a list of decorators
delete(relation_id, id)[source]
get(relation_id, id=None)[source]
methods = {'DELETE', 'GET', 'POST', 'PUT'}
post(relation_id)[source]
put(relation_id, id)[source]

The CollectionPropertyResource class

class flask_restalchemy.resources.CollectionPropertyResource(declarative_model, related_model, property_name, serializer, session_getter, query_modifier=None, request_decorators=None)[source]

Bases: flask_restalchemy.resources.resources.ToManyRelationResource

get(relation_id, id=None)[source]
methods = {'DELETE', 'GET', 'POST', 'PUT'}
post(relation_id)[source]