0

I want to return GenericDeleteView with arguments from another view.

I have a view, that gets an pk of an object and I want to delete it using Django generic DeleteView subclass.

The problem is that the pk can be of different Models.

I have the respective GenericDeleteViews defined and if I add them to urls.py and trigger them from there with positional argument, everything works fine. But I would like something little different.

Example of what I would want:

views.py

def delete_object_view(request, pk):
if FirstModel.objects.filter(pk=pk).exists():
    return FirstModelDeteleView.as_view()(request, !!pk!!)
else:
    return SecondModelDeleteView.as_view()(request, !!pk!!)

But the case is that this does not pass the pk to the DeleteView and it returns the error:
Generic delete view must be called with either an object pk or a slug

I tried many alternatives, passing kwargs={'pk':pk} and some other, but nothing seems to be working.

I am desperate and even tough I know some workarounds, this sounds like something that should be possible, and seems elegant.

Thanks for any advice.

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52

2 Answers2

0

From the docs:

You should use the reverse() urlresolver.

Try this:

from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

    if FirstModel.objects.filter(pk=pk).exists():
        return HttpResponseRedirect('first_model_delete',kwargs={'pk' : pk})
    else:
        return HttpResponseRedirect('second_model_delete',kwargs={'pk' : pk})
arcegk
  • 1,480
  • 12
  • 15
  • Correct me if I am wrong. The problem with this solution is that you end up having an url, which has a view that has to decide an then redirect to the specific delete_url of a Model. That way you end up with three different urls.. – Ondrej Husár Feb 04 '16 at 11:45
  • You're right, with this solution you need 3 different url. – arcegk Feb 04 '16 at 14:50
0

I will not follow your approach in calling another view from a view. (i find it is not elegant solution)

I have not test this answer, so you may need to debug some minor errors later.

Because your DeleteView may use different Model, then you may want to determine the Model dynamically.

You still could use the generic DeleteView. Since it uses the SingleObjectMixin, instead of specifying the model in the View, you should then overwrite get_queryset method or get_object method.

Sample code:

class MyDeleteView(DeleteView):
    def get_queryset(self):
        if FirstModel.objects.filter(pk=<pk>).exists():
            return FirstModel.objects.all()
        else:
            return SecondModel.objects.all()
Community
  • 1
  • 1
Yeo
  • 11,416
  • 6
  • 63
  • 90
  • 1
    Thank you very much. Just to clarify to other people, you give the parameter to objects.filter function from DeleteView as so: `Model.objects.filter(pk=self.kwargs.get(self.pk_url_kwarg))` And in the template use {{ object. }} to access the models attributes. – Ondrej Husár Feb 04 '16 at 11:42
  • yup, something like that. – Yeo Feb 04 '16 at 16:33