Ticket #3683: better_permalink.patch

File better_permalink.patch, 2.8 KB (added by Chris Beaven, 18 years ago)
  • 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        reverse_kwargs = {}
     32        if isinstance(bits, (list, tuple)):
     33            viewname = bits[0]
     34            if len(bits) >= 3:
     35                # For backwards compatibility, check for any third argument and
     36                # use instead of the second if given - was previously used for
     37                # the dict of kwargs.
     38                args = bits[2]
     39            elif len(bits) == 2:
     40                args = bits[1]
     41        else:
     42            viewname = bits
     43        if args:
     44            if isinstance(args, dict):
     45                reverse_kwargs['kwargs'] = args
     46            else:
     47                reverse_kwargs['args'] = args
     48        return reverse(viewname, **reverse_kwargs)
    2949    return inner
    3050
    3151class 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