djangorest_alchemy package¶
Submodules¶
djangorest_alchemy.fields module¶
Relationship field
djangorest_alchemy.inspector module¶
Functions to reflect over SQLAlchemy models
- exception djangorest_alchemy.inspector.KeyNotFoundException[source]¶
Bases: exceptions.Exception
Primary key not found exception
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
Example:
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
- class djangorest_alchemy.mixins.MultipleObjectMixin[source]¶
Bases: object
SQLAlchemy analog to Django’s MultipleObjectMixin.
- allow_empty = True¶
- filter_query_object(query_object)[source]¶
Generic filtering.
This is a stub and has yet to be implemented.
- get_paginate_by(query_object)[source]¶
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¶
- paginator_class¶
alias of Paginator
- query_object = None¶
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
- get_other_pks(request)[source]¶
Return default empty {} Override to return back your primary keys from other source (possibly from headers)
Parameters: request – REST request object Returns: dict
- 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/“
}
]
}
Note:
* 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“}
Note:
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.