djangorest_alchemy package


djangorest_alchemy.fields module

Relationship field

class djangorest_alchemy.fields.AlchemyRelatedField(*args, **kwargs)[source]

Bases: rest_framework.relations.RelatedField

class djangorest_alchemy.fields.AlchemyUriField(*args, **kwargs)[source]

Bases: rest_framework.relations.RelatedField


djangorest_alchemy.inspector module

Functions to reflect over SQLAlchemy models

exception djangorest_alchemy.inspector.KeyNotFoundException[source]

Bases: exceptions.Exception

Primary key not found exception


This is a utility function to get the attribute names for the primary keys of a class

# >>> class_keys(Deal) # >>> (‘dealer_code’, ‘deal_jacket_id’, ‘deal_id’)


Utility function to get the primary key of the class. In case of multiple primary keys, use the <classname>_id convention

djangorest_alchemy.managers module

Base for interfacing with SQLAlchemy Provides the necessary plumbing for CRUD using SA session

class djangorest_alchemy.managers.AlchemyModelManager(*args, **kwargs)[source]

Bases: object

list(other_pks=None, filters=None)[source]

List returns back list of URI In case of multiple pks, We guess the pk by using ‘<modelname>_id’ as the field convention

retrieve(pks, other_pks=None)[source]

Retrieve fetches the object based on the following pk logic: if ‘other’ pks are not found, just use the pk list (coming from URLS) assuming their order is already correct if ‘other’ pks are found, then use the class keys to get the correct order of pks, look them up

djangorest_alchemy.mixins module

class djangorest_alchemy.mixins.ManagerMeta[source]

Bases: type

Meta class to read action methods from manager and attach them to viewset This allows us to directly call manager methods without writing any action methods on viewsets

class djangorest_alchemy.mixins.ManagerMixin[source]

Bases: object

Manager mixin allows to use a manager class to provide the actual CRUD implementation in addition to providing action methods


class MyManager(AlchemyModelManager):
    action_methods = {'my_method': ['POST']}

    def my_method(self, data, pk=None, **kwargs):
        # data is actual payload
        return {'status': 'created'}

class MyViewSet(viewset.Viewsets, ManagerMixin):
    manager_class = MyManager
manager_factory(*args, **kwargs)[source]

Factory method for instantiating manager class Override to return back your instance

class djangorest_alchemy.mixins.MultipleObjectMixin[source]

Bases: object

SQLAlchemy analog to Django’s MultipleObjectMixin.

allow_empty = True

Generic filtering.

This is a stub and has yet to be implemented.


Returns True to display empty lists, False to 404.


Add the object list to the template context.


Get the number of items to paginate by. None for no pagination.

get_paginator(query_object, per_page, orphans=0, allow_empty_first_page=True)[source]

Get a paginator instance.

The class used is overridable by setting the paginator_class attribute.

paginate_by = None
paginate_query_object(query_object, page_size)[source]

Paginate the query object.


alias of Paginator

query_object = None
djangorest_alchemy.mixins.make_action_method(name, methods, **kwargs)[source]

djangorest_alchemy.routers module

class djangorest_alchemy.routers.ReadOnlyRouter(trailing_slash=True)[source]

Bases: rest_framework.routers.DefaultRouter

A router for read-only APIs, which USES trailing slashes.

routes = [Route(url='^{prefix}{trailing_slash}$', mapping={'get': 'list'}, name='{basename}-list', initkwargs={'suffix': 'List'}), Route(url='^{prefix}/{lookup}{trailing_slash}$', mapping={'get': 'retrieve'}, name='{basename}-detail', initkwargs={'suffix': 'Detail'})]

djangorest_alchemy.serializers module

Base AlchemyModelSerializer which provides the mapping between SQLALchemy and DRF fields to serialize/deserialize objects

class djangorest_alchemy.serializers.AlchemyListSerializer(*args, **kwargs)[source]

Bases: djangorest_alchemy.serializers.AlchemyModelSerializer

base_fields = {}
class djangorest_alchemy.serializers.AlchemyModelSerializer(*args, **kwargs)[source]

Bases: rest_framework.serializers.Serializer

Alchemy -> DRF field serializer

base_fields = {}
field_mapping = {<class 'sqlalchemy.types.CLOB'>: <class 'rest_framework.fields.CharField'>, <class 'sqlalchemy.types.VARCHAR'>: <class 'rest_framework.fields.CharField'>, <class 'sqlalchemy.types.CHAR'>: <class 'rest_framework.fields.CharField'>, <class 'sqlalchemy.types.Float'>: <class 'rest_framework.fields.FloatField'>, <class 'sqlalchemy.types.Boolean'>: <class 'rest_framework.fields.BooleanField'>, <class 'sqlalchemy.types.String'>: <class 'rest_framework.fields.CharField'>, <class 'sqlalchemy.types.DateTime'>: <class 'rest_framework.fields.DateTimeField'>, <class 'sqlalchemy.types.INTEGER'>: <class 'rest_framework.fields.IntegerField'>, <class 'sqlalchemy.types.SMALLINT'>: <class 'rest_framework.fields.IntegerField'>, <class 'sqlalchemy.types.BIGINT'>: <class 'rest_framework.fields.IntegerField'>, <class 'sqlalchemy.types.BigInteger'>: <class 'rest_framework.fields.IntegerField'>, <class 'sqlalchemy.types.TIMESTAMP'>: <class 'rest_framework.fields.DateTimeField'>, <class 'sqlalchemy.types.Numeric'>: <class 'rest_framework.fields.IntegerField'>, <class 'sqlalchemy.types.DATE'>: <class 'rest_framework.fields.DateTimeField'>}

djangorest_alchemy.settings module

djangorest_alchemy.viewsets module

Base AlchemyViewSet which provides the necessary plumbing to interface with AlchemyModelSerializer and AlchemyModelManager

class djangorest_alchemy.viewsets.AlchemyModelViewSet(**kwargs)[source]

Bases: djangorest_alchemy.mixins.MultipleObjectMixin, djangorest_alchemy.mixins.ManagerMixin, rest_framework.viewsets.ViewSet

Generic SQLAlchemy viewset which calls methods over the specified manager_class and uses specified serializer_class

destroy(request, pk=None)[source]

Return default empty {} Override to return back your primary keys from other source (possibly from headers)

Parameters:request – REST request object
get_pks(request, **kwargs)[source]

Return list of pks from the keyword args e.g. /models/pk1/childmodel/pk2 return back [pk1, pk2]

Parameters:request – REST request object
Kwargs kwargs:URI keyword args
Returns:List e.g. [pk1, pk2]
list(request, **kwargs)[source]

Returns back serialized list of objects URIs in the results key

Returns:json {
“results”: [
“href”: “http://server/api/models/pk/





* URI contains the same pk field
* Complete URI with server/port is returned back
retrieve(request, **kwargs)[source]

Retrieve returns back serialized object.

Returns:json {
“href”: “http://server/api/models/pk/”, “field”: “value” “childmodel”: “http://serv/api/parentmodels/pk/childmodels/pk



As of now, only SQLAlchemy mapper properties are returned
No other fields or properties are serialized. You will need
to override retrieve and provide your own implementation to
query those additional properties for now.
serializer_factory(multiple, queryset, model_class, context)[source]

Factory method to instantiate appropriate serializer class Override to return back your instance

update(request, pk=None)[source]

Module contents