Create Django Modules
  • 18 Mar 2024
  • 1 Minute to read
  • Dark
    Light
  • PDF

Create Django Modules

  • Dark
    Light
  • PDF

Article summary

Guidelines

Django guidelines

  • Follow the PEP8 style guide.

  • Add migrations files when your module includes new data models.

  • List your python dependencies on the install_requires argument of setup.py.

  • Test your module on a demo app.

General guidelines

  • Prefer included libraries over adding extra dependencies

  • Test your code. Add unit tests and integration tests, when appropriate

  • Avoid “magic” values. Make your software easy to change and configure.

  • Expose module configuration using module options.

  • Add detailed documentation to the README file for your module.

  • Make your module cross-platform.  (Refer to Platform-specific module code.)

Create a Django module

cb create --name example --type django

The following files are generated.

django-example/
├── django_example
│   ├── pyproject.toml
│   ├── example
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   └── setup.py
└── meta.json

Files

meta.json

This file holds the metadata for the module and includes the  root specifier to identify where the module will be installed.

{  "title": "example",  "description": "",  "root": "/backend/modules",  "schema": {} }
Note

The meta.json file should always be in the root of the module folder. This file is not installed in an app. The meta.json file is the main configuration mechanism where you configure options such as names, titles, and root paths.

pyproject.toml

This file defines the build system.

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

We use setuptools as the build backend.  

setup.py

We configure the package information and build in a setup.py file.

from setuptools import setup
from setuptools.command.build import build

# Override build command
class BuildCommand(build):
    def initialize_options(self):
        build.initialize_options(self)
        self.build_base = "/tmp"

setup(
    name="cb_django_example",
    version="0.1",
    packages=["example"],
    install_requires=[""],
    cmdclass={"build": BuildCommand},
) 

example directory

The example directory contains the basic structure of a Django app generated with python manage.py startapp. Here you can write your module’s source code, create Data Models, include Django migrations, expose endpoints, and much more.

from django.apps import AppConfig

class ExampleConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "modules.django_example.example"

Notice how your module is nested within another Django app called modules. That app is included by default on the scaffold and it includes custom code for automatically loading any module within, automatically loading any urlpatterns or admin configs it can find.


Was this article helpful?