Ticket #3683: better_permalink.2.patch
File better_permalink.2.patch, 2.8 KB (added by , 18 years ago) |
---|
-
django/db/models/__init__.py
16 16 # Admin stages. 17 17 ADD, CHANGE, BOTH = 1, 2, 3 18 18 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 # 21 25 # Returns a function that calls urlresolvers.reverse() on that data, to return 22 26 # the URL for those parameters. 23 27 def permalink(func): 24 28 from django.core.urlresolvers import reverse 25 29 def inner(*args, **kwargs): 26 30 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) 29 50 return inner 30 51 31 52 class LazyDate(object): -
docs/model-api.txt
1757 1757 in the URLConf file and in the model. 1758 1758 1759 1759 You 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:: 1760 decorator. This decorator is passed a string containing the view function 1761 and if necessary, either a list of ordered parameters or a dictionary of 1762 named parameters you would use for accessing this instance directly. Django 1763 then works out the correct full URL path using the URLconf. For example:: 1763 1764 1764 1765 from django.db.models import permalink 1765 1766 1766 1767 def get_absolute_url(self): 1767 return ('people.views.details', str(self.id))1768 return 'people.views.details', {'person_id': self.id} 1768 1769 get_absolute_url = permalink(get_absolute_url) 1769 1770 1770 1771 In this way, you're tying the model's absolute URL to the view that is used