Skip to main content

a Django package for scaffolding django rest apis using cli

Project description

Overview

This library will help you to scaffold full Restful API Resources in seconds using only one command:

$ python manage.py dr_scaffold blog Post body:textfield author:foreignkey:Author

🎉 Your RESTful Post api resource is ready 🎉
  • models.py with Models and fields generated by the CLI ⚡

  • admin.py with Models registered and ready ⚡

  • views.py with appropriate ViewSets ready⚡

  • urls.py with appropriate URLs ready.⚡

  • serializers.py with Model Serializers ready ⚡

  • and more …

Installation and usage

For a detailed guide read scaffold django apis like a champion

This library assumes that you have setup your project with Django Rest Framework. if not, please refer to this guide

1- Install dr_scaffold package :

$ pip install dr-scaffold

2- Add dr_scaffold to your INSTALLED_APPS like this:

INSTALLED_APPS = [
    ...
    'dr_scaffold'
]

3- If you are using a custom API structure, add CORE_FOLDER and API_FOLDER to your settings.py:

#include a forward slash at the end of each

CORE_FOLDER = "my_core_folder/" # you can leave them empty
API_FOLDER = "my_api_folder/"   # or set them to be the same

4- Run the your scaffolds like this:

$ python manage.py dr_scaffold blog Post body:textfield author:foreignkey:Author --mixins CRUD

🎉 Your RESTful Post api resource is ready 🎉

Supported ViewSet types

We support two types of ViewSets, we support ModelViewSet and we support ViewSets with Mixins.

  • ModelViewSets are the default that get generated with the dr_scaffold command

  • To generate a view with Mixins pass a value of what mixins you want to include like --mixins CRUD this will result in a view with the Create, List, Retrieve, Update, Destroy actions.

Let’s generate an API that does only support the Create and Read methods (Read is both list and retrieve):

$ python manage.py dr_scaffold blog Author name:charfield --mixins CR

🎉 Your RESTful Post api resource is ready 🎉

The command will generate an Author API with a ViewSet like the following:

class AuthorViewSet(
    mixins.CreateModelMixin,
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    viewsets.GenericViewSet
):
    queryset = Author.objects.all()
    serializer_class = AuthorSerializer
    #permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        #user = self.request.user
        queryset = Author.objects.all()
        #insert specific queryset logic here
        return queryset

    def get_object(self):
        #insert specific get_object logic here
        return super().get_object()

    def create(self, request, *args, **kwargs):
        serializer = AuthorSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = AuthorSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = AuthorSerializer(instance=instance)
        return Response(serializer.data)

Supported field types

We support most of django field types.

TODO

  • add an option to include swagger documentation

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

dr_scaffold-2.0.0.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

dr_scaffold-2.0.0-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file dr_scaffold-2.0.0.tar.gz.

File metadata

  • Download URL: dr_scaffold-2.0.0.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.5

File hashes

Hashes for dr_scaffold-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b9d770e59054445804ce7b305b5d7040c7e171cba69a2e93cf28d1c040f7c33f
MD5 e1eb3cc2136caca090e8d71425f125c2
BLAKE2b-256 3798f5a3926758146cbdf5a376e7374081bab63f32115c9b26fc75b35ca0fcfc

See more details on using hashes here.

File details

Details for the file dr_scaffold-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: dr_scaffold-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.5

File hashes

Hashes for dr_scaffold-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 921eed89c1046520e9eeb94f60e4b0371c9bee9df5fda4ae5d55dbf62229692b
MD5 10e7afaea5fcce047144f8d44f3b0a41
BLAKE2b-256 c976018ae49d3d0e8f6cca4dd6a384d69992630dce6c627b7d4ae702f5163fe0

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