Code Monkey home page Code Monkey logo

drf-generators's Introduction

DRF Generators

Writing APIs can be boring and repetitive work. Don't write another CRUDdy view in Django Rest Framework. With DRF Generators, one simple command will generate all of your Views, Serializers, and even Urls for your Django Rest Framework application!

For a full step-by-step tutorial, check out my blog post!

This is not intended to give you a production quality API. It was intended to jumpstart your development and save you from writing the same code over and over for each model.


Supported Python versions Latest Version License Travis CI Django 1.7, 1.8 DRF 3.0, 3.1



Installation

Install with pip:

$ pip install drf-generators

or Clone the repo and install manually:

$ git clone https://github.com/brobin/drf-generators.git
$ cd drf-generators
$ python setup.py install

To use DRF Generators, add it your INSTALLED_APPS.

INSTALLED_APPS = (
    ...
    'rest_framework',
    'drf_generators',
    ...
)

Note: In order to use the APIView classes, you must have the rest framework DEFAULT_PAGINATION_CLASS and PAGE_SIZE set.

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 15
}

Usage

To use the generators, run the following command, where app is the application to generate an API for.

$ python manage.py generate {app} {options}
Option Action
--serializers Generate only Serializers for your app.
--views Generate only Views for your app.
--urls Generate only urls for your app.
--force Overwrite existing files without the warning prompt.
-f, --format Format to use when generating views and urls. Valid options: viewset, apiview, function, modelviewset. Default: viewset.
-d, --depth Serialization depth for related models. Default: 0

Example: Generate everything for the app api with function style views, overwriting existing files, with a serialization depth of 2.

$ python manage.py generate api --format function --force -- depth=2

Serializers

Drf Generators will create serializers.py for your application. It currently uses rest framework's ModelSerializer for serialization of the models defined in models.py.

class ModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = User

Views

DRF Generators will create views.py for your application. It can generate ViewSet, APIView and function based views. Set the --format option when running the generator to pick the preferred style

ViewSet

python manage.py generate api --format viewset

class ModelViewSet(ViewSet):

    def list(self, request):
        ...
    def create(self, request):
        ...
    def retrieve(self, request, pk=None):
        ...
    def update(self, request, pk=None):
        ...
    def destroy(self, request, pk=None):
        ...

APIView

python manage.py generate api --format apiview

class ModelAPIView(APIView):

    def get(self, request, id, format=None):
        ...
    def put(self, request, id, format=None):
        ...
    def delete(self, request, id, format=None):
        ...

class ModelAPIListView(APIView):

    def get(self, request, format=None):
        ...
    def post(self, request, format=None):
        ...

Function

python manage.py generate api --format function

@api_view(['GET', 'POST'])
def model_list(request):
    if request.method == 'GET':
        ...
    elif request.method == 'POST':
        ...

@api_view(['GET', 'PUT', 'DELETE'])
def model_detail(request, pk):
    if request.method == 'GET':
        ...
    elif request.method == 'PUT':
        ...
    elif request.method == 'DELETE':
        ...

ModelViewSet

python manage.py generate api --format modelviewset

class MyModelViewSet(ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

Urls

Finally, DRF Generator will create you a default urls.py to match the View format you are using.

ViewSet & ModeViewSet Routes

router = SimpleRouter()

router.register(r'model', views.ModelViewSet, 'Model')

urlpatterns = router.urls

APIView urls

url(r'^model/([0-9]+)$', views.ModelAPIView.as_view()),
url(r'^model', views.ModelAPIListView.as_view()),

Function urls

urlpatterns = [

    url(r'^model/(?P<pk>[0-9]+)$', views.model_detail),
    url(r'^model/$', views.model_list),

]

urlpatterns = format_suffix_patterns(urlpatterns)

Tests

A full application built with drf-generators can be found in the tests directory. Instructions on running the tests can be found in the test project's README.

License

MIT License. See LICENSE.

drf-generators's People

Contributors

brobin avatar guest007 avatar jeverling avatar

Watchers

 avatar  avatar

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.