Phil Sheard
Phil Sheard

Reputation: 2162

Django admin view without model

Sorry, I don't have a code sample yet as I'm trying to work out if what I'm thinking can even be done.

I'm writing a Django app to manage Celery tasks. I'm using django-celery-beat for scheduled tasks but I'd like a similar admin interface to launch ad-hoc tasks with a form to accept parameters. I'd like it to sit with IP the admin section as this is all the app does; there are no other views right now.

All of the docs for ModelAdmin are based around custom fields for models but I don't think this object needs to be stored in a model as they are transient and data will be saved by the Celery task. From what I understand from the docs, you can't register a custom admin view without trying it to a model.

What I'm looking for is a custom view that's not tied to a model, just so I can create a custom form for these management commands in the admin section.

Ideas I've had:

Any pointers appreciated. I'll test all the reasonable answers and select the best as the right answer but will upvote for all responses.

Edit: @dejavu_cmd_delt made the great suggestion of Flower but I'd like to keep it all integrated within the admin section if possible.

Upvotes: 6

Views: 1656

Answers (1)

Phil Sheard
Phil Sheard

Reputation: 2162

I gave up :) It seemed too much of an anti-pattern so I abandoned the idea and created a simple custom model based on the one in django_celery_beat for ad-hoc tasks. This allowed me to piggy back on the task detection methods for the ScheduledTask modelform and use the pre_save hook to issue the task on creation to Celery. I then created a separate Django app to contain it and install alongside the main django_celery_beat.

Here's a copy of the model in case it's ever of use to anyone:

class AdhocTask(models.Model):

    id = models.AutoField(primary_key=True)
    description = models.CharField(blank=True, max_length=255)
    regtask = models.CharField(null=True, max_length=200)
    args = models.TextField(blank=True, default='[]',
                            help_text='JSON encoded positional args'
                            )
    kwargs = models.TextField(blank=True, default='{}',
                            help_text='JSON encoded keyword args'
                            )


# SendAdhocTask is my management command that parses the AdhocTask instance
# and sends the task to a Celery task queue.
signals.pre_save.connect(SendAdhocTask, sender=AdhocTask)

Upvotes: 1

Related Questions