Skip to content

Latest commit

 

History

History
80 lines (60 loc) · 1.45 KB

test.md

File metadata and controls

80 lines (60 loc) · 1.45 KB

Quick start

Create django app

./manage startapp myapp

myapp/urls.py

from django_declarative_apis import adapters
import myapp.resources

urlpatterns = [
    url(
        r'^users$',
        adapters.resource_adapter(
            get=myapp.resources.UserListDefinition,
            post=myapp.resources.UserCreateDefinition
        )
    ),
    url(
        r'^ping$',
        adapters.resource_adapter(
            get=myapp.resources.PongDefinition
        )
    ),
]

myapp/resources.py

from django_declarative_apis import machinery


class PongDefinition(machinery.EndpointDefinition):
    """ Example of an endpoint not tied to a model
    """
    def is_authorized(self):
        return True

    @property
    def resource(self):
        return {'ping': 'pong'}


class UserListDefinition(machinery.ResourceEndpointDefinition):
    """ Example of a get/list endpoint tied to a model
    """
    resource_model = modes.User

    def is_authorized(self):
        return True


class UserCreateDefinition(machinery.ResourceEndpointDefinition):
    """ Example of a create endpoint tied to a model
    """
    resource_model = models.User

    def is_authorized(self):
        return True

myapp/models.py

from django.db import models


class User(models.Model):
    name = models.CharField(max_length=100, null=False)