[Fixed]-Django – Custom admin action

1πŸ‘

βœ…

The queryset parameter to the admin action already contains the selected projects. Alter to:

def show_gantt_chart_of_selected_projects(modeladmin, request, queryset):
    ct = ContentType.objects.get_for_model(queryset.model)  # why do you do this, you're not using it?
    return HttpResponseRedirect("/xxx/?ct=%s&ids=%s" % (ct.pk, ",".join(queryset.values_list('pk', flat=True))) 

BTW you should use reverse url resolving instead of hardcoding urls.

View, which I took the liberty to switch to a class-based version. You will want to do this eventually anyway:

from django.views.generic import ListView

class IndexView(ListView):
    template_name = 'xxx/ganttChart.html'
    context_object_name = 'projects'
    model = Project

    def get_queryset(self):
        return Project.objects.filter(
            pk__in=self.request.GET.get('ids','').split(','),
        ).order_by('projectName')
index = IndexView.as_view()
πŸ‘€Tomas Walch

Leave a comment