This is old and in some way ugly-code keeping repository that illustrates simple idea how to apply mass actions upon several entries without getting formsets. Please treat this is like a poor proof-of-concept, yet another thing it uses pretty old django version however you’re still able to get through code and pick ideas up.
Django-action is a small and simple application which would help you to implement actions similar to django.contrib.admin case for your custom tasks outside django admin. Also it provides custom form support if you would like to change some data for the queryset instances.
Sample with approval and custom forms you will find withing example app into the repository.
You may run
$ python manage.py syncdb
To add initial data and then start sample server:
python manage.py runserver localhost:8000
Superuser account: super, super
Guest account: guest, guest
Fast model instances deletion (without form’s approval):
Model:
from django.db import models
from apps.files.actions import force_delete_action
class File(models.Model):
title = models.CharField('Title', max_length=128)
file = models.FileField(upload_to='media/files')
actions = [force_delete_action,]
django-action (apps.files.actions):
def force_delete_action(request, qset, model, **kwargs):
for q in qset:
q.file.delete() #deletes file
q.delete() #deletes instance
return {'qset': qset}
force_delete_action.short_description = 'force delete files'
Template:
{% extends "base.html" %} {% load actiontags %}
{% block content %}
{% if posts %}
{# {% get_action_list for 'example.post' as actions %} #} {# works as well as listed below #}
{% get_action_list for posts as actions %}
{% if actions.object_list %}
<form action='{% url url_act actions.action %}' method='post' id='id_action_posts'>
{% csrf_token %}
{% show_actions actions.object_list %}
{% endif %}
{% for p in posts %}
<li>{% if actions.object_list %}
<input name='items' value='{{ p.id }}' type='checkbox' class='void'>
{% endif %}
{{ p.title }}: {{ p.category }}
<div class='content'>
{{ p.content }}
<div class='author'>{{ p.author }} ({{ p.ip_addr }})</div>
</div>
</li>
{% endfor %}
{% if actions.object_list %}
<input type='submit' value='submit'>
</form>
{% endif %}
{% else %}
<h3>It might be usefull to add some posts ;), don't you find it?</h3>
{% endif %}
{% endblock %}
Django-action – небольшое приложение, которое повторяет возможности actions django.contrib.admin вне django админки.
См. выше