In cases like this, rather than rely on overriding the default behavior of 
the DeleteView,
I'd recommend writing your own base class.

It'll be more explicit and more obvious exactly what is going on...

    from django.views.generic import View

    class MarkDeletedView(View):
        """
        Base class for marking a model instance as deleted.
        """
        model = None
        redirect_url = None

        def delete(self, request, *args, **kwargs):
            assert self.model, "model attribute must be set"
            assert self. redirect_url, "redirect_url attribute must be set"

            object = get_object_or_404(self.model, pk=kwargs['pk'])
            if object.date_deleted is not None:
                raise Http404
            now = datetime.datetime.utcnow().replace(tzinfo=utc)
            object.date_deleted = now.strftime("%Y-%m-%d %H:%M:%S")
            object.save()
            return HttpResponseRedirect(self.redirect_url)

Then you can create a different view for each model you need this behavior 
on, like so...

    class CustomerDeleteView(MarkDeletedView):
        model = Customer
        redirect_view = reverse('customer-list')

Hope that helps.

  Tom

On Thursday, 21 March 2013 12:39:34 UTC, Christian Schmitt wrote:

> Hello guys,
> I have a question about the django CBV DeleteView, I want to use it but 
> not delete the element, i just want to set a delete_date so that it isn't 
> visible anymore.
>
> With FBV my view looked like this:
>
> def delete(request, customer_id):
>     customer = get_object_or_404(Customer, pk=customer_id)
>     if customer.date_deleted is not None:
>         raise Http404
>     now = datetime.datetime.utcnow().replace(tzinfo=utc)
>     customer.date_deleted = now.strftime("%Y-%m-%d %H:%M:%S")
>     customer.save()
>     return HttpResponseRedirect()
>
> but know I want to change all my Views with CBVs but I really don't 
> understand how I could change the default query on DeleteView. Is there 
> somebody who could help me with it?
>
> I mean it is way easier than to Delete items, since I have one DeleteView 
> where I could use to Delete all items.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to