Ticket #2568: permalink.diff

File permalink.diff, 1.4 KB (added by Joeboy, 9 years ago)
  • model-api.txt

     
    16801680
    16811681    <a href="{{ object.get_absolute_url }}">{{ object.name }}</a>
    16821682
    1683 (Yes, we know ``get_absolute_url()`` couples URLs to models, which violates the
    1684 DRY principle, because URLs are defined both in a URLconf and in the model.
    1685 This is a rare case in which we've intentionally violated that principle for
    1686 the sake of convenience. With that said, we're working on an even cleaner way
    1687 of specifying URLs in a more DRY fashion.)
    16881683
     1684The ``permalink()`` decorator
     1685-----------------------------
     1686
     1687The trouble with the use of ``get_absolute_url()`` above is that hard coding
     1688parts of your urls couples URLs to models, which violates the DRY principle,
     1689because URLs are defined both in a URLconf and in the model.
     1690
     1691In the development version, you can further decouple your apps using the
     1692``permalink`` decorator. for example::
     1693
     1694    from django.db.models import permalink
     1695
     1696    def get_absolute_url(self):
     1697        return ('people.views.detail', str(self.id))
     1698    get_absolute_url=permalink(get_absolute_url)
     1699
     1700The decorated function does a reverse lookup to calculate the url for the
     1701object based on view and parameters returned by the undecorated function, and
     1702your project / apps' urlconfs.
     1703
     1704
    16891705Executing custom SQL
    16901706--------------------
    16911707
Back to Top