Create templates

A template is a directory with files that will be copied to the final path in your app. Template files can use Django Templates Syntax. When templates are generated, the app models are available to be used with the django template syntax.

For example if you create the template mytemplate you can use it for your app *myapp^with the command:

$ python manage.py generate mytemplate myapp

Template locations

There are several places to locate the directories for your templates. The templates will be loaded in this order:

  1. Template directories in DJANGO_CODE_GENERATOR_TEMPLATES environment variable. The directories are separated by a colon char (:). For example: DJANGO_CODE_GENERATOR_TEMPLATES=/path/templates/.

  2. Templates from the folder .dcg_templates/ in the current directory.

  3. Template directories from DJANGO_CODE_GENERATOR_TEMPLATES = [] list in your Django settings.

  4. Templates from folder .dcg_templates/ in manage.py directory.

  5. Templates from ~/.config/dcg_templates/ directory.

  6. Templates from django_code_generator project

To create the template, make a directory with the name of the template in the templates folder. For example: ~/.config/dcg_templates/mytemplate/. When you use the command manage.py generate <template> <project app> everything inside the template folder will be copied and rendered to the app folder in your Django project.

For example, running manage.py generate mytemplate myapp the file ~/.config/dcg_templates/mytemplate/admin.py will be copied and rendered to myproject/myapp/admin.py.

Template context

These variables are available during rendering:

  • models: a list of the models in the app. See django_code_generator.models.Models class. The listing contains django_code_generator.models.Model instances.

  • app: a AppConfig instance.

You can also use the included template tags. To use the tags you must put at the beginning of the template {% load code_generator_tags %}.

Examples

admin/__init__.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{%  load code_generator_tags %}from django.contrib import admin
{% from_module_import app.name|add:'.models' models %}{% comment %}
{% endcomment %}
{% for model in models %}

@admin.register({{ model.name }})
class {{ model.name }}Admin(admin.ModelAdmin):
    """
    """
    list_display = (
        {% indent_items model.filter_field_names 8 quote='simple' %}
    )
    search_fields = (
        {% indent_items model.char_field_names 8 quote='simple' %}
    )
    {% if model.foreign_field_names %}autocomplete_fields = (
        {% indent_items model.foreign_field_names 8 quote='simple' %}
    ){% endif %}{% endfor %}
api/__init__.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{%  load code_generator_tags %}from django.conf.urls import url, include
from rest_framework import routers

{% from_module_import app.name|add:'.api.viewsets' models|add_to_items:'ViewSet' %}


router = routers.DefaultRouter()
{% for model in models %}
router.register(r'{{ model.snake_case_name }}s', {{ model }}ViewSet){%endfor%}

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
]
api/serializers.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{%  load code_generator_tags %}from rest_framework.serializers import ModelSerializer
{% from_module_import app.name|add:'.models' models %}{% comment %}
{% endcomment %}{% for model in models %}


class {{ model.name }}Serializer(ModelSerializer):
    class Meta:
        model = {{ model.name }}
        depth = 1
        fields = (
            {% indent_items model.field_names 12 quote='simple' %}
        )
        read_only_fields = (){% comment %}
{% endcomment %}{% endfor %}
api/viewsets.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
{%  load code_generator_tags %}from rest_framework import viewsets
{% from_module_import app.name|add:'.models' models %}
{% from_module_import app.name|add:'.api.serializers' models|add_to_items:'Serializer' %}{% comment %}
{% endcomment %}{% for model in models %}


class {{ model.name }}ViewSet(viewsets.ModelViewSet):
    """
    """
    queryset = {{ model.name }}.objects.all()
    serializer_class = {{ model.name }}Serializer
    search_fields = (
        {% indent_items model.string_field_names 8 quote='simple' %}
    )
    filter_fields = (
        {% indent_items model.filter_field_names 8 quote='simple' %}
    )
    ordering_fields = (
        {% indent_items model.concrete_field_names 8 quote='simple' %}
    ){% comment %}
{% endcomment %}{% endfor %}