Skip to main content

Untangle your Django models

Project description

Django Comb

Django Comb is a command-line tool aimed at untangling Django Models.

It provides a lint_models management command. This is a linter, allowing developers to define rules about their models.

Currently, the only rule is the no-inbound-foreign-keys rule, which prevents other models from defining ForeignKey, OneToOneField or ManyToManyField into a given model.

Usage

1. Install django_comb

Ensure django_comb is on the Python path and add it to INSTALLED_APPS:

# myproject/settings.py

INSTALLED_APPS = [
   "django_comb",
   ...
]

2. Define rules

  • Choose an installed Django app to place your rules. (It doesn't matter functionally, but usually you will want to use the app that contains the models you are protecting.)
  • Create a model_rules.toml in the directory in that app, alongside the app's models.py.
  • Add one or more rule definitions, in the form [rule.my-rule-id]. The second part of the TOML key (after the .) is your rule's unique id. See Supported Rules for examples.

3. Run the linter

Run the lint_models management command:

python manage.py lint_models

Supported rules

No inbound foreign keys

Prevents other models from defining a ForeignKey, OneToOneField or ManyToManyField into a set of models.

TOML Key Type Description
type String The rule type, must be "no_inbound_foreign_keys".
models String or Array of Strings The models to protect against inbound foreign keys pointing to them from other models, in the form app_label.ModelName. Supports Unix shell-style wildcards.
allowed String or Array of Strings (optional) Models that are allowed to point to models, in the form app_label.ModelName. Supports Unix shell-style wildcards.
silenced_violations String or Array of Strings (optional) Functionally identical to allowed, but indicates undesirable dependencies. Supports Unix shell-style wildcards.

Example

# path/to/some_app/model_rules.toml

[rule.no-fks-to-blue-or-green]
type = "no_inbound_foreign_keys"
models = "some_app.*"  # All models in some_app.
allowed = [
    # It's okay for these models to have a foreign key
    # to the models above.
    "some_app.*",
    "another_app.Orange",
]
# Models listed here also point to the models above, but they shouldn't.
# Including them here suppresses violations in the same way as
# `allowed`, but indicates that the violation is technical debt
# rather than intentional design.
silenced_violations = "another_app.Purple"

Exit codes

lint_models exits with the following status codes:

  • 0 if no violations were found.
  • 1 if violations were found.
  • 2 if it terminates abnormally, e.g. due to invalid configuration.

The get_model_rules command

To view the discovered model rules as JSON, run the get_model_rules management command:

$ python manage.py get_model_rules
[
  {
    "id": "all",
    "app_label": "another_app",
    "type": "no_inbound_foreign_keys",
    "models": [
      "some_app.*"
    ],
    "allowed": [
      "some_app.*",
      "another_app.Pur*"
    ],
    "silenced_violations": [
      "another_*.Orange"
    ],
    "extra_key_one": "foo",
    "extra_key_two": "bar"
  },
  {
    "id": "some-app-yellow",
    "app_label": "some_app",
    "type": "no_inbound_foreign_keys",
    "models": [
      "some_app.Yellow"
    ]
  }
]

This can be useful for building secondary tooling that tracks, say, the number of silenced_violations over time.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_comb-1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_comb-1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file django_comb-1.0.tar.gz.

File metadata

  • Download URL: django_comb-1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_comb-1.0.tar.gz
Algorithm Hash digest
SHA256 4887d04b53c7eb0a54f7d1ed3c925065a7a350445d1558a9fdc6e3d874d04c9c
MD5 b3177a096c467f33a6872588cb4e62f5
BLAKE2b-256 e01ac5665d8f021fdff64aeae1a7a67aea003171dba9be41082901fea16a131d

See more details on using hashes here.

File details

Details for the file django_comb-1.0-py3-none-any.whl.

File metadata

  • Download URL: django_comb-1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_comb-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6677bf819aafdff641f4f1efa1b53fc5cc7543f2acacf2e6f212d868d4e9f8f
MD5 098161eefc29bd9849492e88c8761b50
BLAKE2b-256 62d3c50891c06fda38e6119ab9c92da0611ac8cefed09fed31e5497d08b164e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page