a command line utility to avoid repetitive work when creating django rest framework views, serializers and urls
A package that makes for you the repetitive work of creating Serializers, ViewSets and URLs for django rest framework.
On a virtualenv run:
pip install django-easy-drf
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
If you want to avoid command prompt, you can force it:
easy-drf --force
or less verbose:
easy-drf -f
Sometimes you don’t need all files (views, serializers and urls) so you can specify which ones should be created. Options are:
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.
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
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:
This is the default behavior, but an option to rewrite the entire file will be added soon.
For help, type:
easy-drf --help
or less verbose:
easy-drf -h
This command will list the available cli options.
If you have an idea or an implementation, let me know by submitting an issue or a PR.