Created
August 24, 2017 22:02
-
-
Save adrianomargarin/1dd4db616c9a1eaddbcc1a8e319ba55e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# admin.py | |
from django.shortcuts import render | |
from django.http import HttpResponseRedirect | |
from django import forms | |
class MyFormAdmin(forms.Form): | |
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput) | |
filefield = forms.FileField(label='File') | |
@admin.register(MyModel) | |
class MyModelAdmin(admin.ModelAdmin): | |
actions = ['my_action'] | |
def my_action(self, request, queryset): | |
form = None | |
if 'save' in request.POST: | |
form = MyFormAdmin(request.POST) | |
if form.is_valid(): | |
form.save() | |
# Your magic | |
self.message_user(request, 'Success message') | |
return HttpResponseRedirect(request.get_full_path()) | |
if not form: | |
form = MyFormAdmin(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)}) | |
context = {'objects': queryset, 'form': form} | |
return render(request, 'admin/my_template.html', context) | |
my_action.short_description = 'My Action' | |
# admin/my_template.htm | |
{% extends "admin/base_site.html" %} | |
{% block content %} | |
<form action="." method="post"> | |
{% csrf_token %} | |
{{ form }} | |
<input type="hidden" name="action" value="my_action" /> | |
<input type="submit" name="save" value="Save" /> | |
</form> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment