Code

Opened 6 years ago

Closed 6 years ago

#7196 closed (wontfix)

Callback function for date_based.object_detail function

Reported by: bogdan.agica@… Owned by: nobody
Component: Generic views Version: master
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: UI/UX:

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 (0)

Change History (3)

comment:1 Changed 6 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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

comment:2 Changed 6 years ago by bogdan.agica@…

  • Resolution invalid deleted
  • Status changed from closed to reopened

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 Changed 6 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from reopened to closed

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 Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.