Package: django-easy-drf
django-easy-drfA package that makes for you the repetitive work of creating Serializers, ViewSets and URLs for django rest framework.InstallationOn a virtualenv run:pip install django-easy-drfUsageOn the same virtualenv, you will haveeasy-drfcommand available, so run:easy-drfThis 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:fromdjango.dbimportmodelsclassExampleModel(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:fromrest_frameworkimportserializersfrom.modelsimportExampleModelclassExampleModelSerializer(serializers.ModelSerializer):classMeta():model=ExampleModelfields=('id','some_field','some_other_field','third_field')The resulting views.py will be like this:fromrest_frameworkimportviewsetsfrom.serializersimportExampleModelSerializerfrom.modelsimportExampleModelclassExampleModelViewSet(viewsets.ModelViewSet):queryset=ExampleModel.objects.all()serializer_class=ExampleModelSerializerThe resulting urls.py will be like this:fromrest_framework.routersimportDefaultRouterfrom.viewsimportExampleModelViewSetrouter=DefaultRouter()router.register('example-model',ExampleModelViewSet,basename='example-model')urlpatterns=router.urlsForcing creationIf you want to avoid command prompt, you can force it:easy-drf --forceor less verbose:easy-drf -fCreating only one fileSometimes 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.pyFor example, this command will create serializers.py and views.pyeasy-drf --files s vIf you don't specify --files argument, all files will be created.Creating only one modelSometimes 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:fromdjango.dbimportmodelsclassExampleModel(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)classDogModel(models.Model):name=models.DateTimeField(verbose_name='Horario de evento')age=models.TextField(default='Titulo evento')is_good_boy=models.BooleanField()But you know thatonly DogModel will be serialized, so you can create a serializer only for this model, running:easy-drf --files s -m DogModelThe resulting serializers.py file will be like this:fromrest_frameworkimportserializersfrom.modelsimportDogModelclassDogModelSerializer(serializers.ModelSerializer):classMeta():model=DogModelfields=('id','some_field','some_other_field','third_field')You can use -m or --models and specify some models:easy-drf --files s --models DogModel ExampleModelAppend by defaultIf 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:fromdjango.dbimportmodelsclassExampleModel(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)classDogModel(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:fromrest_frameworkimportserializersfrom.modelsimportExampleModelclassExampleModelSerializer(serializers.ModelSerializer):classMeta():model=ExampleModelfields=('id','some_field','some_other_field','third_field')Then you runeasy-drf -m DogModel --files sThe resulting serializers.py file will be like this:fromrest_frameworkimportserializersfrom.modelsimportExampleModel,DogModelclassExampleModelSerializer(serializers.ModelSerializer):classMeta():model=ExampleModelfields=('id','some_field','some_other_field','third_field')classDogModelSerializer(serializers.ModelSerializer):classMeta():model=DogModelfields=('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 contentThis is the default behavior, but an option to rewrite the entire file will be added soon.Getting helpFor help, type:easy-drf --helpor less verbose:easy-drf -hThis command will list the available cli options.Future developmentsWarn 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.ContributingIf you have an idea or an implementation, let me know by submitting an issue or a PR.