Skip to main content

A package to create boring stuff of django rest framework

Project description

django-easy-drf

A package that makes for you the repetitive work of creating Serializers, ViewSets and URLs for django rest framework.

Installation

On a virtualenv run:

pip install django-easy-drf

Usage

On the same virtualenv, you will have easy-drf command available, so run:

easy-drf

This command will create serializers.py, views.py and urls.py files on the same directory, with all the classes created.

Note: requires that a file called models.py exists on the current directory.

Suppose you have a models.py file with the following content:

from django.db import models

class ExampleModel(models.Model):
    some_field = models.IntegerField()
    some_other_field = models.DecimalField(decimal_places=2, max_digits=10)
    third_field = models.DecimalField(decimal_places=2, max_digits=10)

The resulting serializers.py will be like this:

from rest_framework import serializers
from .models import ExampleModel

class ExampleModelSerializer(serializers.ModelSerializer):

    class Meta():
        model = ExampleModel
        fields = ('id', 'some_field', 'some_other_field', 'third_field')

The resulting views.py will be like this:

from rest_framework import viewsets
from .serializers import ExampleModelSerializer
from .models import ExampleModel

class ExampleModelViewSet(viewsets.ModelViewSet):
    queryset = ExampleModel.objects.all()
    serializer_class = ExampleModelSerializer

The resulting urls.py will be like this:

from rest_framework.routers import DefaultRouter
from .views import ExampleModelViewSet
router = DefaultRouter()
router.register('example-model', ExampleModelViewSet, basename='example-model')
urlpatterns = router.urls

Forcing creation

If you want to avoid command prompt, you can force it:

easy-drf --force

or less verbose:

easy-drf -f

Creating only one file

Sometimes you don't need all files (views, serializers and urls) so you can specify which ones should be created. Options are:

  • 's' for serializers.py
  • 'v' for views.py
  • 'u' for urls.py

For example, this command will create serializers.py and views.py

easy-drf --files s v

If you don't specify --files argument, all files will be created.

Creating only one model

Sometimes you don't need all models, so you can specify which ones should be created.

Suppose you have a models.py file with the following content:

from django.db import models

class ExampleModel(models.Model):
    some_field = models.IntegerField()
    some_other_field = models.DecimalField(decimal_places=2, max_digits=10)
    third_field = models.DecimalField(decimal_places=2, max_digits=10)


class DogModel(models.Model):
    name = models.DateTimeField(verbose_name='Horario de evento')
    age = models.TextField(default='Titulo evento')
    is_good_boy = models.BooleanField()

But you know that only DogModel will be serialized, so you can create a serializer only for this model, running:

easy-drf --files s -m DogModel

The resulting serializers.py file will be like this:

from rest_framework import serializers
from .models import DogModel

class DogModelSerializer(serializers.ModelSerializer):

    class Meta():
        model = DogModel
        fields = ('id', 'some_field', 'some_other_field', 'third_field')

You can use -m or --models and specify some models:

easy-drf --files s --models DogModel ExampleModel
Append by default

If any of the named files exist on the current directory, the result will be appended to their content. Suppose you have a models.py file with the following content:

from django.db import models

class ExampleModel(models.Model):
    some_field = models.IntegerField()
    some_other_field = models.DecimalField(decimal_places=2, max_digits=10)
    third_field = models.DecimalField(decimal_places=2, max_digits=10)

class DogModel(models.Model):
    name = models.DateTimeField(verbose_name='Horario de evento')
    age = models.TextField(default='Titulo evento')
    is_good_boy = models.BooleanField()

And you have a serializers.py file like this:

from rest_framework import serializers
from .models import ExampleModel

class ExampleModelSerializer(serializers.ModelSerializer):

    class Meta():
        model = ExampleModel
        fields = ('id', 'some_field', 'some_other_field', 'third_field')

Then you run

easy-drf -m DogModel --files s

The resulting serializers.py file will be like this:

from rest_framework import serializers
from .models import ExampleModel, DogModel

class ExampleModelSerializer(serializers.ModelSerializer):

    class Meta():
        model = ExampleModel
        fields = ('id', 'some_field', 'some_other_field', 'third_field')

class DogModelSerializer(serializers.ModelSerializer):

    class Meta():
        model = DogModel
        fields = ('id', 'some_field', 'some_other_field', 'third_field')

The effect of the command was:

  • In the second line of the serializers.py file, DogModel was added as an import.
  • DogModelSerializer is created at the bottom of the file, keeping the original file's content

This is the default behavior, but an option to rewrite the entire file will be added soon.

Getting help

For help, type:

easy-drf --help

or less verbose:

easy-drf -h

This command will list the available cli options.

Future developments

  • Warn users about files override.
  • Allow users to force script without prompt.
  • Allow users to create just one specific file.
  • Allow users to create just one model.
  • Allow users to override default append behavior.
  • Allow users to specify different models file.
  • Allow users to specify different results file names.

Contributing

If you have an idea or an implementation, let me know by submitting an issue or a PR.

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-easy-drf-0.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

django_easy_drf-0.1.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file django-easy-drf-0.1.0.tar.gz.

File metadata

  • Download URL: django-easy-drf-0.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.1

File hashes

Hashes for django-easy-drf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7b3b7fe00f466f944eae85dcfdbb092e5c893c82f9149d4895bc041a0507b8ea
MD5 1ad4f0af76b96ec9f6ab5d2b3421e832
BLAKE2b-256 31d4a0a351fd8f0c1394cf67c6b308ac1822aeb3e59a6c7a6573acb14d51ca4d

See more details on using hashes here.

File details

Details for the file django_easy_drf-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_easy_drf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.1

File hashes

Hashes for django_easy_drf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf34daa88b164a9d3296e4f0a45bb682e73d80ce67fcc62dbe6e7c7146882b9f
MD5 f9f592befe6bc8dacb55ed8c0d85197e
BLAKE2b-256 e364fb7bd46b4b736248c3f293ddc1d8bb649454f9a522a52abe159965d761db

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