PK->objects in view signature

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def sigtransform(*Models):
    import django
    from django.shortcuts import get_object_or_404
    def _decorator(func):
        def _closure(request, *args, **kwargs):
            args = list(args)
            for ii, Model_and_cleaner in enumerate(Models):
                # None is passed to @sigtransform to indicate "leave
                # this argument alone"
                if Model_and_cleaner is None:
                    pass
                else:
                    Model, cleaner = Model_and_cleaner
                    # pk arg needs correct datatype, hence cleaner func. 
                    args[ii] = get_object_or_404(Model, pk=cleaner(args[ii]))
            return func(request, *args, **kwargs)
        _closure.__name__ = func.__name__
        _closure.__doc___ = func.__doc__
        _closure.__module___ = func.__module__
        return _closure
    return _decorator

More like this

  1. Other approach of making middleware (by decorators) by diverman 3 years, 1 month ago
  2. Render Decorator by dahool 4 years, 7 months ago
  3. render_to by asolovyov 5 years, 10 months ago
  4. Allow separation of GET and POST implementations by agore 1 year, 10 months ago
  5. Format transition middleware by limodou 7 years, 1 month ago

Comments

(Forgotten your password?)