Ticket #3683: better_permalink.2.patch

File better_permalink.2.patch, 2.8 KB (added by Chris Beaven, 18 years ago)

Uh, I meant this one

  • django/db/models/__init__.py

     
    1616# Admin stages.
    1717ADD, CHANGE, BOTH = 1, 2, 3
    1818
    19 # Decorator. Takes a function that returns a tuple in this format:
    20 #     (viewname, viewargs, viewkwargs)
     19# Decorator. Takes a function that returns a string containing the viewname
     20# or a tuple in this format:
     21#     (viewname, viewargs)
     22# Where viewargs is either a dictionary of named arguments or a list
     23# of ordered arguments.
     24#
    2125# Returns a function that calls urlresolvers.reverse() on that data, to return
    2226# the URL for those parameters.
    2327def permalink(func):
    2428    from django.core.urlresolvers import reverse
    2529    def inner(*args, **kwargs):
    2630        bits = func(*args, **kwargs)
    27         viewname = bits[0]
    28         return reverse(bits[0], None, *bits[1:3])
     31        args = None
     32        reverse_kwargs = {}
     33        if isinstance(bits, (list, tuple)):
     34            viewname = bits[0]
     35            if len(bits) >= 3:
     36                # For backwards compatibility, check for any third argument and
     37                # use instead of the second if given - was previously used for
     38                # the dict of kwargs.
     39                args = bits[2]
     40            elif len(bits) == 2:
     41                args = bits[1]
     42        else:
     43            viewname = bits
     44        if args:
     45            if isinstance(args, dict):
     46                reverse_kwargs['kwargs'] = args
     47            else:
     48                reverse_kwargs['args'] = args
     49        return reverse(viewname, **reverse_kwargs)
    2950    return inner
    3051
    3152class LazyDate(object):
  • docs/model-api.txt

     
    17571757in the URLConf file and in the model.
    17581758
    17591759You can further decouple your models from the URLconf using the ``permalink``
    1760 decorator. This decorator is passed the view function and any parameters you
    1761 would use for accessing this instance directly. Django then works out the
    1762 correct full URL path using the URLconf. For example::
     1760decorator. This decorator is passed a string containing the view function
     1761and if necessary, either a list of ordered parameters or a dictionary of
     1762named parameters you would use for accessing this instance directly. Django
     1763then works out the correct full URL path using the URLconf. For example::
    17631764
    17641765    from django.db.models import permalink
    17651766
    17661767    def get_absolute_url(self):
    1767         return ('people.views.details', str(self.id))
     1768        return 'people.views.details', {'person_id': self.id}
    17681769    get_absolute_url = permalink(get_absolute_url)
    17691770
    17701771In this way, you're tying the model's absolute URL to the view that is used
Back to Top