Verified Commit 61308a17 authored by Micheal Gendy's avatar Micheal Gendy
Browse files

Add option to exclude fields

parent 07a5aa26
......@@ -83,13 +83,15 @@ Option Action
``--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
``--exclude`` Exclude fields from model
``-m`` , ``--model`` Model to exclude fields from
========================== ===================================================
**Example:** Generate everything for the app ``api`` with function style views, overwriting existing files, with a serialization depth of 2.
.. code-block:: bash
$ python manage.py generate api --format function --force --depth=2
$ python manage.py generate api --format function --force --depth=2 --exclude field1,field2 -m User
-------------------
......
from django.template import Template, Context
from django.utils.html import escape
import os.path
from drf_generators.templates.serializer import SERIALIZER
......@@ -22,8 +23,14 @@ class BaseGenerator(object):
self.models = self.get_model_names()
self.serializers = self.get_serializer_names()
def generate_serializers(self, depth):
content = self.serializer_content(depth)
def add_quote(self,var):
return '"{0}"'.format(var)
def generate_serializers(self, depth , exclude , model):
list_fields = tuple(map(list,exclude))
list_models = tuple(map(list,model))
merged_fields_models = list(zip(list_fields, list_models))
content = self.serializer_content(depth,merged_fields_models)
filename = 'serializers.py'
if self.write_file(content, filename):
return ' - writing %s' % filename
......@@ -46,9 +53,9 @@ class BaseGenerator(object):
else:
return 'Url generation cancelled'
def serializer_content(self, depth):
def serializer_content(self, depth,merged_fields_models):
context = Context({'app': self.name, 'models': self.models,
'depth': depth})
'depth': depth , 'merged_fields_models':merged_fields_models})
return self.serializer_template.render(context)
def view_content(self):
......
......@@ -19,6 +19,12 @@ class Command(AppCommand):
parser.add_argument('-d', '--depth', dest='depth', default=0,
help='serialization depth'),
parser.add_argument('--exclude' , action="append",nargs='+',
dest='exclude', help="exclude fields from model"),
parser.add_argument('-m','--model',dest='model',action="append",nargs='+',
help="model to exclude fields from")
parser.add_argument('--force', dest='force', action='store_true',
help='force overwrite files'),
......@@ -49,6 +55,8 @@ class Command(AppCommand):
serializers = options['serializers']
views = options['views']
urls = options['urls']
exclude = options['exclude']
model = options['model']
else:
raise CommandError('You must be using Django 1.11, 2.2, or 3.0')
......@@ -66,13 +74,16 @@ class Command(AppCommand):
raise CommandError(message)
if serializers:
result = generator.generate_serializers(depth)
if exclude and not model :
message = "You must choose model to exclude fields from"
raise CommandError(message)
result = generator.generate_serializers(depth,exclude.model)
elif views:
result = generator.generate_views()
elif urls:
result = generator.generate_urls()
else:
result = generator.generate_serializers(depth) + '\n'
result = generator.generate_serializers(depth,exclude,model) + '\n'
result += generator.generate_views() + '\n'
result += generator.generate_urls()
......
__all__ = ['SERIALIZER']
SERIALIZER = """from rest_framework.serializers import ModelSerializer
from {{ app }}.models import {{ models | join:', ' }}
{% load convert_tags %}
{% for model in models %}
class {{ model }}Serializer(ModelSerializer):
......@@ -11,4 +14,10 @@ class {{ model }}Serializer(ModelSerializer):
model = {{ model }}{% if depth != 0 %}
depth = {{ depth }}{% endif %}
fields = '__all__'
{% for i in merged_fields_models %}{% if i.1|join:", " == model %}{% with field=i.0|join:"''" %}
exclude = ({{field|add_quotes|safe}})
{% endwith %}
{% endif %}
{% endfor %}
{% endfor %}"""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment