项目作者: pablop94

项目描述 :
a command line utility to avoid repetitive work when creating django rest framework views, serializers and urls
高级语言: Python
项目地址: git://github.com/pablop94/django-easy-drf.git
创建时间: 2020-12-22T03:15:48Z
项目社区:https://github.com/pablop94/django-easy-drf

开源协议:GNU General Public License v3.0

下载


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:

  1. pip install django-easy-drf

Usage

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

  1. 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:

  1. from django.db import models
  2. class ExampleModel(models.Model):
  3. some_field = models.IntegerField()
  4. some_other_field = models.DecimalField(decimal_places=2, max_digits=10)
  5. third_field = models.DecimalField(decimal_places=2, max_digits=10)

The resulting serializers.py will be like this:

  1. from rest_framework import serializers
  2. from .models import ExampleModel
  3. class ExampleModelSerializer(serializers.ModelSerializer):
  4. class Meta():
  5. model = ExampleModel
  6. fields = ('id', 'some_field', 'some_other_field', 'third_field')

The resulting views.py will be like this:

  1. from rest_framework import viewsets
  2. from .serializers import ExampleModelSerializer
  3. from .models import ExampleModel
  4. class ExampleModelViewSet(viewsets.ModelViewSet):
  5. queryset = ExampleModel.objects.all()
  6. serializer_class = ExampleModelSerializer

The resulting urls.py will be like this:

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

Forcing creation

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

  1. easy-drf --force

or less verbose:

  1. 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

  1. 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:

  1. from django.db import models
  2. class ExampleModel(models.Model):
  3. some_field = models.IntegerField()
  4. some_other_field = models.DecimalField(decimal_places=2, max_digits=10)
  5. third_field = models.DecimalField(decimal_places=2, max_digits=10)
  6. class DogModel(models.Model):
  7. name = models.DateTimeField(verbose_name='Horario de evento')
  8. age = models.TextField(default='Titulo evento')
  9. 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:

  1. easy-drf --files s -m DogModel

The resulting serializers.py file will be like this:

  1. from rest_framework import serializers
  2. from .models import DogModel
  3. class DogModelSerializer(serializers.ModelSerializer):
  4. class Meta():
  5. model = DogModel
  6. fields = ('id', 'some_field', 'some_other_field', 'third_field')

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

  1. 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:

  1. from django.db import models
  2. class ExampleModel(models.Model):
  3. some_field = models.IntegerField()
  4. some_other_field = models.DecimalField(decimal_places=2, max_digits=10)
  5. third_field = models.DecimalField(decimal_places=2, max_digits=10)
  6. class DogModel(models.Model):
  7. name = models.DateTimeField(verbose_name='Horario de evento')
  8. age = models.TextField(default='Titulo evento')
  9. is_good_boy = models.BooleanField()

And you have a serializers.py file like this:

  1. from rest_framework import serializers
  2. from .models import ExampleModel
  3. class ExampleModelSerializer(serializers.ModelSerializer):
  4. class Meta():
  5. model = ExampleModel
  6. fields = ('id', 'some_field', 'some_other_field', 'third_field')

Then you run

  1. easy-drf -m DogModel --files s

The resulting serializers.py file will be like this:

  1. from rest_framework import serializers
  2. from .models import ExampleModel, DogModel
  3. class ExampleModelSerializer(serializers.ModelSerializer):
  4. class Meta():
  5. model = ExampleModel
  6. fields = ('id', 'some_field', 'some_other_field', 'third_field')
  7. class DogModelSerializer(serializers.ModelSerializer):
  8. class Meta():
  9. model = DogModel
  10. 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:

  1. easy-drf --help

or less verbose:

  1. 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.