Thursday, March 21, 2013

Re: Django 1.5 CBV - DeleteView - CustomQuery

Oh, yeah you gave me a good idea, but no, DELETE is fine. Since I wanted to do an ajax request anyway.

Am Donnerstag, 21. März 2013 14:22:59 UTC+1 schrieb Tom Christie:
Actually you'll probably want the `delete` method to be a `post` instead,
but either way hopefully it'll give you the right idea.

On Thursday, 21 March 2013 13:11:44 UTC, Tom Christie wrote:
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+unsubscribe@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.
 
 

No comments:

Post a Comment