Hi all, I just wrote a decorator to validate if an object(say Blog) exists:
def require_blog_item_exists(func): def _decorated_func(request, blog_id, *args, **kwargs): try: blog = Blog.objects.get(id = blog_id) return func(request, blog_id, *args, **kwargs) except: return HttpResponseRedirect('error') return _decorated_func An idea pops into my mind, that why not cache the blog object in func.func_globals to avoid extra database retrieval operation? So I wrote following codes: from new import function as new_function def update_func(func, extra_globals): new_globals = func.func_globals.copy() new_globals.update(extra_globals) return new_func(func.func_code, new_globals, func.func_name, func.func_defaults, func.func_closure) def require_blog_item_exists(func): def _decorated_func(request, blog_id, *args, **kwargs): try: blog = Blog.objects.get(id = blog_id) f = update_func_globals(func, {'blog':blog}) return f(request, blog_id, *args, **kwargs) except: return HttpResponseRedirect('error') return _decorated_func However, it also reports error. Can anyone explain to me what's wrong with the codes? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---