• Stars
    star
    154
  • Rank 234,608 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 2 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

scaffold django rest apis like a champion ๐Ÿš€

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]

More Repositories

1

tcp-tac-toe

a peer to peer TCP tic tac toe game in plain Golang
Go
6
star
2

Abdenasser

4
star
3

flasklearning

a little crud application for Flask learning
Python
2
star
4

RNTravelJournal-concept

a react native Travel Journal application UI concept inspired by this nice design @dribbble https://dribbble.com/shots/15004647
TypeScript
2
star
5

geChallenge

Angular application for listing most starred Github repos that were created in the last 30 days.
TypeScript
1
star
6

todo

reactjs project using firebase
JavaScript
1
star
7

pupsub

a nodejs pub sub server
JavaScript
1
star
8

abdenasser.github.io-old

JavaScript
1
star
9

ReactJS

first steps
JavaScript
1
star
10

http_server_with_ruby

Ruby
1
star
11

peepchat

an emberjs ui for a phoenix api
JavaScript
1
star
12

foodrecipesapi

1
star
13

ImageStreaming

a nodeJS app for streaming images in real-time, through the pub/sub server https://github.com/Abdenasser/pupsub
JavaScript
1
star
14

store-client

this is an ember client with a rails backend (https://github.com/Abdenasser/store-api) i created for a learning purpose
JavaScript
1
star
15

invoices-tracker-ember

JavaScript
1
star
16

peepchat-api

phoenix api
JavaScript
1
star
17

mizo

a fuse mobile project for a little presentation
1
star
18

postcss-tryout

HTML
1
star
19

DjangoRestFrameworkExperiments

experimenting features of django rest framework
Python
1
star
20

twitter-clone

Nodal is a (rails-like) web server for nodejs, this repo is just for test!
JavaScript
1
star
21

intro-hapi-js-part1

JavaScript
1
star
22

timeline

a real-time timeline using rethinkDB hapiJS and nes plugin
JavaScript
1
star
23

store-api

this is the rails api for the ember client https://github.com/Abdenasser/store-client
Ruby
1
star
24

Elixir-works

some examples of elixir code
Elixir
1
star
25

express-test

let's try ExpressJS
JavaScript
1
star
26

imgur

this is a reactjs app that uses imgur api (photos sharing)
JavaScript
1
star