Skip to main content

django-namespaces

Use namespaces in requests using Django.

Motivation

With django-namespaces, we get a new way to group resources based on request.namespace.

Installation

Use a virtual environment whenever using Python packages. The built-in venv module is great.

python3 -m venv venv
source venv/bin/activate
$(venv) python -m pip install django-namespaces --upgrade

Django Settings (settings.py)

Installed Apps

Add django_namespaces to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_namespaces",
]

Middleware

Update MIDDLEWARE:

MIDDLEWARE = [
    # ...
    "django_namespaces.middleware.NamespaceMiddleware",
]

This gives us access to the request.namespace object in our views.

Template Context Processors

Add django_namespaces.context_processors.user_namespaces to TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS = [
    # ...
    "django_namespaces.context_processors.user_namespaces",
]

Usage Usage

from django.contrib.auth import get_user_model
from django_namespaces.models import Namespace

User = get_user_model()
user = User.objects.create_user(username="jon.snow", password="youknowsomething")

namespace = Namespace.objects.create(handle="winterfell", user=user)
namespace2 = Namespace.objects.create(handle="thewall", user=user)
import django_namespaces

django_namespaces.activate("winterfell")

This will add a namespace to the request object.

def my_hello_world_view(request):
    print(request.namespace)  # <Namespace: winterfell>
    print(request.namespace.handle)  # winterfell
    return HttpResponse("Hello World")
from django.db import models
from django_namespaces.models import Namespace


class Location(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    namespace = models.ForeignKey(Namespace, on_delete=models.CASCADE)

Custom Namespace Model

If you want to use a custom namespace model, you can do so by setting the DJANGO_NAMESPACES_NAMESPACE_MODEL setting.

DJANGO_NAMESPACES_NAMESPACE_MODEL = "orgs.Organization"

Example model in orgs.models.py:

from django.db import models


class Organization(models.Model):
    handle = models.CharField(max_length=255, unique=True)
  • handle is the only required field to swap the model.

Optional Views

Using views are optional. You can also use the activate function to activate a namespace.

Update URLconf

Update urls.py to include namespaces.urls:

urlpatterns = [
    # ...
    path("namespaces/", include("django_namespaces.urls")),
]

Create a Namespace

Create a namespace by visiting http://localhost:8000/namespaces/create/ and filling out the form.

Activate a Namespace

Activate a namespace by visiting http://localhost:8000/namespaces/ and hitting activate on your newly created namespace.

You can also use:

Update URLconf

Metadata

Release files for django-namespaces 0.0.27

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-namespaces 0.0.27
File Size Uploaded Provenance
django_namespaces-0.0.27.tar.gz 38.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-namespaces 0.0.27
File Interpreter ABI Platform
django_namespaces-0.0.27-py3-none-any.whl Python 3 none any Details

Total release size: 61.0 kB

Release files / django_namespaces-0.0.27.tar.gz

Download URL django_namespaces-0.0.27.tar.gz
Size 38.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9f80ac164e081c0f0917ee45444adaeeb6c493f4379170b273377f3f0b67f75a
BLAKE2b-256 checksum
How to use checksums
fef49596c3c196fc15286cc52dabe4d1e7ef792e63bd0826e697fec9546df3fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 19, 2025.

Transparency log

Release files / django_namespaces-0.0.27-py3-none-any.whl

Download URL django_namespaces-0.0.27-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df519e5e76270b2181445cb595ea734b0b1a0d1ff5f387909a21a44b03e22cf3
BLAKE2b-256 checksum
How to use checksums
35f65fe6794b1622652a9e60be388e25e224c0b10db1eb60734ce3f3bf2ab42a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 19, 2025.

Transparency log

Security for django-namespaces 0.0.27

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_namespaces-0.0.27.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 19, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page