Opened 17 years ago

Closed 17 years ago

#7196 closed (wontfix)

Callback function for date_based.object_detail function

Reported by: bogdan.agica@… Owned by: nobody
Component: Generic views Version: dev
Severity: Keywords: date_based callback object_detail
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

There might be the need of implementing a callback function in the object_detail function. This shoud come after the object query, and before the template rendering, and it might be used for extra processing, such as incrementing a number_of_views counter, for example.

--- django/django/django/views/generic/date_based.py	2007-12-21 12:12:16.000000000 +0200
+++ date_based.py	2008-05-08 16:50:26.000000000 +0300
@@ -290,7 +290,7 @@
         month_format='%b', day_format='%d', object_id=None, slug=None,
         slug_field='slug', template_name=None, template_name_field=None,
         template_loader=loader, extra_context=None, context_processors=None,
-        template_object_name='object', mimetype=None, allow_future=False):
+        template_object_name='object', mimetype=None, allow_future=False, callback_function=None):
     """
     Generic detail view from year/month/day/slug or year/month/day/id structure.
 
@@ -326,6 +326,8 @@
         obj = queryset.get(**lookup_kwargs)
     except ObjectDoesNotExist:
         raise Http404, "No %s found for" % model._meta.verbose_name
+    if callable(callback_function):
+        callback_function(obj)
     if not template_name:
         template_name = "%s/%s_detail.html" % (model._meta.app_label, model._meta.object_name.lower())
     if template_name_field:

Change History (3)

comment:1 by James Bennett, 17 years ago

Resolution: invalid
Status: newclosed

This can be accomplished with a wrapper around the generic view.

comment:2 by bogdan.agica@…, 17 years ago

Resolution: invalid
Status: closedreopened

I meant there might be the case for processing something on the object before it's displayed -- in my example, incrementing a number of views _of the actual object_. If this is to be done in the wrapper, it would require duplicating the object query code -- (copying a large chunk of the code in date_based.object_detail, e.g)

comment:3 by James Bennett, 17 years ago

Resolution: wontfix
Status: reopenedclosed

If that's the case you probably shouldn't be using a generic view; once the necessary logic reaches that point you really ought to be writing your own view. The alternative is for generic views to grow increasingly cluttered with trying to support situations further and further away from their (often very simple) purposes.

Note: See TracTickets for help on using tickets.
Back to Top