Ticket #3683: better_permalink.patch
File better_permalink.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 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) 29 49 return inner 30 50 31 51 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