Skip to main content

A tiny library to make writing CBV-based APIs easier in Django.

Project description

django-microapi

A tiny library to make writing CBV-based APIs easier in Django.

Essentially, this just provides some sugar on top of the plain old django.views.generic.base.View class, all with the intent of making handling JSON APIs easier (without the need for a full framework).

Usage

from django.contrib.auth.decorators import login_required

from microapi import ApiView, ModelSerializer

from .models import BlogPost


# Inherit from the `ApiView` class...
class BlogPostView(ApiView):
    # ...then define `get`/`post`/`put`/`delete`/`patch` methods on the
    # subclass.

    # For example, we'll provide a list view on `get`.
    def get(self, request):
        posts = BlogPost.objects.all().order_by("-created")

        # The `render` method automatically creates a JSON response from
        # the provided data.
        return self.render({
            "success": True,
            "posts": self.serialize_many(posts),
        })

    # And handle creating a new blog post on `post`.
    @login_required
    def post(self, request):
        # Read the JSON
        data = self.read_json(request)

        # TODO: Validate the data here.

        # Use the included `ModelSerializer` to load the user-provided data
        # into a new `BlogPost`.
        serializer = ModelSerializer()
        post = serializer.from_dict(BlogPost(), data)
        # Don't forget to save!
        post.save()

        return self.render({
            "success": True,
            "post": self.serialize(post),
        })

Installation

$ pip install django-microapi

License

New BSD

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-microapi-1.0.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

django_microapi-1.0.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file django-microapi-1.0.0.tar.gz.

File metadata

  • Download URL: django-microapi-1.0.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for django-microapi-1.0.0.tar.gz
Algorithm Hash digest
SHA256 97ddb941b419ea5cdd7c40132c85f0bfd99a8865b68fd701011f5b99f311e3d3
MD5 892ba8a25b1238fd7810ba66fbc1c177
BLAKE2b-256 a3202baa9743d48b3d277bf86ab5832a622f49fbbcb77f22c8cbae0e512c3bab

See more details on using hashes here.

File details

Details for the file django_microapi-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_microapi-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b24f965c12e4e6674eeaa1efc787ef796f0d8916a1c56ed51e67e7fcb2dd81d
MD5 c05f1570c6bf2269fc24c8797e26d5d7
BLAKE2b-256 1e62fd0744a0645a9fa71389ffd91049cfa9f3c396322be133b78098cdaa03e7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page