Skip to main content

A collection of Django utilities: Admin mixins, testing helpers, and common patterns

Project description

Django Toolkit

A collection of Django utilities: Admin mixins, testing helpers, and common patterns.

Installation

# From GitHub
uv add git+https://github.com/froggen/django-toolkit.git

# Local development
uv add --editable ../django-toolkit

Modules

toolkit.admin - Admin Mixins

from django.contrib import admin
from toolkit.admin import OwnerMixin, ReadOnlyMixin
from unfold.admin import ModelAdmin

@admin.register(Project)
class ProjectAdmin(OwnerMixin, ModelAdmin):
    owner_field = "user"  # Users can only edit their own projects

toolkit.testing - Testing Utilities

from toolkit.testing import has_index_on_columns, get_table_constraints

# Verify database indexes exist
def test_status_index_exists():
    assert has_index_on_columns("invoice", ["status", "created_at"])

Available Admin Mixins

Permission Control

Mixin Description
SuperuserOnlyMixin Only superusers can access
ReadOnlyMixin No add/change/delete, view only
CRUDMixin Basic CRUD for active users
OwnerMixin Users can only access their own data
ReadOnlyOwnerMixin View-only for owned data

Special Patterns

Mixin Description
ActionsOnlyMixin Hide save buttons, use actions only
SingleObjectMixin Redirect to edit/add page (for one-per-user models)

Inline Mixins

Mixin Description
InlineCRUDMixin CRUD for inline models
ReadOnlyInlineMixin Read-only inline

UI Helpers

Mixin Description
HideRelatedFieldButtonsMixin Hide add/edit/delete buttons on FK fields

Testing Utilities

Function Description
get_table_constraints(table_name) Get all constraints and indexes for a table
has_index_on_columns(table_name, columns) Check if an index exists on specified columns

Usage Examples

Owner-based Permission

@admin.register(Article)
class ArticleAdmin(OwnerMixin, ModelAdmin):
    owner_field = "author"
    list_display = ["title", "author", "created_at"]

Nested Owner Field

@admin.register(Comment)
class CommentAdmin(OwnerMixin, ModelAdmin):
    owner_field = "post__author"  # Comment -> Post -> Author

Read-only with Owner Filter

@admin.register(Order)
class OrderAdmin(ReadOnlyOwnerMixin, ModelAdmin):
    owner_field = "customer"

Single Object (e.g., User Profile)

@admin.register(UserProfile)
class UserProfileAdmin(SingleObjectMixin, OwnerMixin, ModelAdmin):
    owner_field = "user"

Actions Only (e.g., Approval Workflow)

@admin.register(WithdrawalRequest)
class WithdrawalRequestAdmin(ActionsOnlyMixin, ModelAdmin):
    readonly_fields = ["amount", "status", "created_at"]
    actions_submit_line = ["approve_action", "reject_action"]

Index Validation in Tests

import pytest
from toolkit.testing import has_index_on_columns

@pytest.mark.django_db
class TestDatabaseIndexes:
    def test_composite_index_exists(self):
        assert has_index_on_columns("booking", ["status", "start"])

    def test_single_column_index_exists(self):
        assert has_index_on_columns("payment", ["transaction_id"])

License

MIT

Project details


Download files

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

Source Distribution

cuz_toolkit-0.1.0.tar.gz (89.0 kB view details)

Uploaded Source

Built Distribution

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

cuz_toolkit-0.1.0-py3-none-any.whl (59.2 kB view details)

Uploaded Python 3

File details

Details for the file cuz_toolkit-0.1.0.tar.gz.

File metadata

  • Download URL: cuz_toolkit-0.1.0.tar.gz
  • Upload date:
  • Size: 89.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for cuz_toolkit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ffcd8246b087a39226896d8877990996c8d90893682d62bb3baa3adf759f9fe2
MD5 c7bf0876e92e82fc76e55e8f60318131
BLAKE2b-256 486b0b028583ecee2643793d896d77270ee9f6284dcb6096ab0387a30c4a6b93

See more details on using hashes here.

File details

Details for the file cuz_toolkit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cuz_toolkit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a2178ea5e45416a21eb656b5ead9a8e6bb4f9300400d5d7b31ea6bd90226d75
MD5 9501bb37ba5bb9e78e1933dca01edde9
BLAKE2b-256 652e3020c33d71619e6299f7ec4c6b87ae93d6b23165c377ecbfd0fcad550349

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