Skip to main content

Extension for the Django admin panel that makes it possible to add actions that do not require a queryset to run.

Project description

django-no-queryset-admin-actions

Extension for the Django admin panel that makes it possible to add actions that do not require a queryset to run.

Works with django-admin-action-forms.

🔌 Installation

  1. Install using pip:

    $ pip3 install django-no-queryset-admin-actions
    
  2. Add 'django_no_queryset_admin_actions' to your INSTALLED_APPS setting.

    INSTALLED_APPS = [
        ...
        'django_no_queryset_admin_actions',
    ]
    

✏️ Examples

No queryset actions

Let's say you have an action that fetches external orders from an API. You don't need a queryset to run this action, but Django requires it by default. By using the @no_queryset_action, you can bypass that, and create actions that can be run without selecting any objects.

from django.contrib.admin import ModelAdmin, register

from django_no_queryset_admin_actions import NoQuerySetAdminActionsMixin, no_queryset_action


@register(ExternalOrder)
class ExternalOrderAdmin(NoQuerySetAdminActionsMixin, ModelAdmin):

    @no_queryset_action(description="Fetch external orders")
    def fetch_external_orders(self, request): # <- No `queryset` parameter
        ...

    actions = [fetch_external_orders]

Optional queryset actions

Another use case is when you have an action that can be run on a specific queryset, but you also want to allow running it without selecting any objects.

This type of action could be used to e.g.:

  • default to all, or a filtered subset of objects when no selection is made
  • choose a random object if no specific item is selected

Leveraging the @optional_queryset_action decorator, you can create actions that run whether or not objects are selected.

Let's say, that ExternalOrder objects fetched in the previous example may sometimes need updating. In this example, if no objects are selected, queryset will be empty, and the action will default to updating all ExternalOrder objects.

from django.contrib.admin import ModelAdmin, register

from django_no_queryset_admin_actions import NoQuerySetAdminActionsMixin, optional_queryset_action


@register(ExternalOrder)
class ExternalOrderAdmin(NoQuerySetAdminActionsMixin, ModelAdmin):

    @optional_queryset_action(description="Update external orders")
    def update_external_orders(self, request, queryset): # <- `queryset` can be empty

        if not queryset:
            queryset = ExternalOrder.objects.all()

        ...

    actions = [update_external_orders]

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_no_queryset_admin_actions-1.2.0.tar.gz (627.5 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file django_no_queryset_admin_actions-1.2.0.tar.gz.

File metadata

File hashes

Hashes for django_no_queryset_admin_actions-1.2.0.tar.gz
Algorithm Hash digest
SHA256 61ad0666c66fcc93175da383c3d80a8616e49d859487830235b327a0bdb707b1
MD5 e9edf59da7be38443c7516e0f2c8451e
BLAKE2b-256 e57adb7605ba4b32c209602d90dc97995113d4ae596c9e0788d02fa4931f1048

See more details on using hashes here.

File details

Details for the file django_no_queryset_admin_actions-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_no_queryset_admin_actions-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0bf3d5c2d998c03c16cbc667c14e9117ef70d13f0b21d4d44581e1f299d4870
MD5 60df14af305fefa0248108c5897afe5c
BLAKE2b-256 92afab4d34032095bcf10ba80cd60f0fa4324db6702d8d84a4e15af3ac3767aa

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