Django

Code

Ticket #7196 (closed: wontfix)

Opened 4 days ago

Last modified 3 days ago

Callback function for date_based.object_detail function

Reported by: bogdan.agica@gmail.com Assigned to: nobody
Component: Generic views Version: SVN
Keywords: date_based callback object_detail Cc:
Triage Stage: Unreviewed Has patch: 1
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

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:

Attachments

Change History

05/08/08 21:13:48 changed by ubernostrum

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to invalid.
  • needs_tests changed.
  • needs_docs changed.

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

05/09/08 02:41:58 changed by bogdan.agica@gmail.com

  • status changed from closed to reopened.
  • resolution deleted.

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)

05/09/08 02:56:32 changed by ubernostrum

  • status changed from reopened to closed.
  • resolution set to wontfix.

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.


Add/Change #7196 (Callback function for date_based.object_detail function)




Change Properties
Action