Skip to main content

Organize Django admin sections/models without additional apps

Project description

codecov pip

 ░▒▓███████▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓█▓▒░      ░▒▓████████▓▒░
░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░
░▒▓█▓▒░      ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░
 ░▒▓██████▓▒░░▒▓████████▓▒░▒▓██████▓▒░ ░▒▓█▓▒░      ░▒▓██████▓▒░
       ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░
       ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░      ░▒▓█▓▒░      ░▒▓█▓▒░
░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓████████▓▒░▒▓█▓▒░

Who?

Django Shelf is meant to be a simple way to categorize your model admins instead of relying on creating new apps for each category.

Bu... but why?

The main reason is simply to avoid creating new apps for each category of model admins. This is especially useful if you got legacy applications that will break if you try to split them into new apps.

Yes, I know that this is not the best practice, but sometimes you just need to do it.

How do I use it?

  1. Install the package using pip install django-admin-shelf.
  2. Add admin_shelf to your INSTALLED_APPS in your Django settings.
  3. Create categories in your admin.py file as follows:
from admin_shelf.admin import Category
from example.models import Model1

custom_category = Category(name="Custom Category")

@custom_category.register(Model1)
class Model1Admin(admin.ModelAdmin):
    pass

A little piece of documentation

The admin_shelf.admin.Category class is used to create categories for your model admins. You can register your model admins to these categories using the @category.register(Model) decorator.

You can set a order for both the categories and the model admins by passing an order argument to the Category class and the @register decorator, respectively.

Example:

from admin_shelf.admin import Category
from example.models import Model1, Model2

custom_category = Category(name="Custom Category", order=1)

@custom_category.register(Model1, order=2)
class Model1Admin(admin.ModelAdmin):
    pass

@custom_category.register(Model2, order=1)
class Model2Admin(admin.ModelAdmin):
    pass

Yes, it is just that simple. You can also use the @register decorator without passing a category, in which case it will be registered to the default category. Or use the default register from django admin if you want to register your model admin without a category.

Before shelf: Before using admin shelf

After shelf: After using admin shelf

Translation support

Category(name=...) accepts both plain strings and Django's lazy translation proxies. To localize a category, pass a gettext_lazy value:

from django.utils.translation import gettext_lazy as _
from admin_shelf.admin import Category

shop_category = Category(name=_("Shop"))

@shop_category.register(Product)
class ProductAdmin(admin.ModelAdmin):
    pass

The name is resolved against the active language on every request, so users switching languages see the category label update accordingly.

Recommendation: define each Category once and reuse the same instance across @register calls. Categories are deduplicated by the identity of the name value; two lazy proxies built from the same source string are distinct Python objects and would create separate buckets.

Anything else?

Running the example app

To run the example app, you can use the following commands:

# You need to have docker installed
docker compose up example

Running the tests

To run the tests, you can use the following commands:

# You need to have docker installed
docker compose up test

How do I contribute?

If you want to contribute to this project, you can do so by creating an issue! And if you want to contribute with code, you can fork the repository and create a pull request.

Check the CONTRIBUTING.md file for more information on how to contribute.

Is it true that bananas are radioactive?

Yes.

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

django_admin_shelf-0.2.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

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

django_admin_shelf-0.2.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_shelf-0.2.0.tar.gz.

File metadata

  • Download URL: django_admin_shelf-0.2.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/7.0.10-zen1-1-zen

File hashes

Hashes for django_admin_shelf-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f1d1cd2866799b4bef9594b735c1b4b28e74edf4d70020001233c9ade968b4cc
MD5 aab5da7bc9c6254d252245041b5977dc
BLAKE2b-256 e03bc8844f7b90e579a564a7c393f36aa0843ab6ee6303c65d00b25402f9726b

See more details on using hashes here.

File details

Details for the file django_admin_shelf-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_admin_shelf-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/7.0.10-zen1-1-zen

File hashes

Hashes for django_admin_shelf-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 534f68213c29eda13031b3243b74fdc4d8fda64cf7f9d9266df8d322f37d806a
MD5 f34a7d522606fb6cb7c36d9469dabd5d
BLAKE2b-256 1ca754f3123f9d44a6b96d5abf9038f96e6f5aee815daf098e91387d70c1062c

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