Giter Site home page Giter Site logo

abdenasser / dr_scaffold Goto Github PK

View Code? Open in Web Editor NEW
154.0 9.0 16.0 15.92 MB

scaffold django rest apis like a champion πŸš€

License: MIT License

Python 97.74% Makefile 2.26%
django rest-api django-rest-framework scaffolding command-line-tool python json-api scaffold generator hacktoberfest web apps

dr_scaffold's Introduction

dr_scaffold blueprint icon

dr_scaffold

Scaffold django rest apis like a champion ⚑. said no one before

Tweet

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
  • 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 Django Rest Framework. if not, please refer to this guide.

Install dr_scaffold package :

$ pip install dr-scaffold

Add dr_scaffold to your INSTALLED_APPS like this:

INSTALLED_APPS = [
    ...
    'dr_scaffold'
]

Add CORE_FOLDER and API_FOLDER to your settings.py (optional):

CORE_FOLDER = "core_dir/"
API_FOLDER = "api_dir/"

Run your scaffolds like this:

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

Generate tests

We support generating tests for your models and apis, you can generate tests by adding --tests to your command like follow:

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

This will generate factories for your models and their tests (ViewSets tests will be added soon), we depend on pytest and factory_boy so you should have them installed in order to run your tests.

Also bare in mind that you should run your migrations before running the tests

Generate ViewSet Mixins

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

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.

Contributors

This project exists thanks to all the people who contribute. [Contribute]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.