Thursday, March 21, 2013

Re: Django 1.5 CBV - DeleteView - CustomQuery

> Is there a site, where I can found such things like What happens in a CBV View?

http://ccbv.co.uk is really great resource.  Can't recommend it highly enough.


On Thursday, 21 March 2013 13:23:05 UTC, Christian Schmitt wrote:
Hi, yeah that definitly helps.
I didn't found a good site where CBVs getting described in that way.
Is there a site, where I can found such things like

What happens in a CBV View? It's way easier to understand the whole CBV thing. 
Btw. FBVs are way easier than CBVs but with FBVs I need to copy & paste things like the MarkDeleteClass while in CBVs I could inherit from it.

Am Donnerstag, 21. März 2013 14:11:44 UTC+1 schrieb Tom Christie:
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