incorporating dejaMoo: best of breed bull…

Django permalink decorator

Sometimes it pays to read the manual

RTFMing

Since I've been spending a lot of time learning Flex I haven't been following Django quite so closely. Finding myself with a dead spot yesterday I figured I'd have a look at updates to the model documentation for changes to model inheritance.

A lack of model inheritance (in SVN) is one of the main barriers to our adopting Django for the full gamut of projects; (n)hiberate often makes too compelling a case which is such a same. However, there is promising talk of SQLAlchemy being used as a drop in replacement for Django's native ORM.

Permalink decorator

Browsing the documentation, I discovered the new(ish) permalink decorator which addresses the accidental coupling of model and view (in the MVC sense):

class Widget(models.Model):
         ...
         def get_absolute_url(self):
             return "/widget/%i/" % self.id

This binds the absolute url of widget to http://example.com/widget/1/ etc. via the model itself. Yuck.

With the new permalink decorator and given a url conf like:

(r'^/widget/(d+)/$', 'widgets.views.detail'),

The absolute url can be decoupled from the model like this:

from django.db.models import permalink
     class Widget(models.Model):
         ...
         def get_absolute_url(self):
             return ('widgets.views.detail', [str(self.id)])
         get_absolute_url = permalink(get_absolute_url)

It's not perfect because the model is now bound to the view (in the Django sense) which now needs to exist wherever the model exists, however it's vastly superior to permanently binding the model to a particular URI.

Django gets more exciting every day.

Comments (2) § Posted by in on
AddThis Social Bookmark Button

Comments 2

  1. Testerofd wrote:

    Helloqmi - this is just a testing, dont worry about it

    Posted July 2, 2007 at 4:50 a.m.
  2. Testerhbm wrote:

    I had enjoyed staying at your site, buy my home page is cooler

    Posted July 2, 2007 at 4:50 a.m.

Comments are now closed.

Tweet Tweet

Stuffs

Thanks for dropping in.

This is the personal website of Cam MacRae. Any opinions expressed here are my entirely own, and have jack to do with my employer.

It's the product of a little elbow grease, the news.ycombinator noprocrast feature, and a healthy dose of Django.

A Django site.

Tags

  1. D (1)
  2. SOA (1)
  3. ajax (2)
  4. apollo (1)
  5. architecture (1)
  6. bemused (1)
  7. blogs (2)
  8. carsales (1)
  9. collaboration (1)
  10. css (1)
  11. django (9)
  12. duels (1)
  13. email (1)
  14. erlang (3)
  15. findability (1)
  16. flex (3)
  17. folksonomies (1)
  18. funny (2)
  19. geek (20)
  20. google (3)
  21. innovation (1)
  22. iphone (1)
  23. javascript (4)
  24. jython (1)
  25. life (5)
  26. lighttpd (1)
  27. lisp (1)
  28. mac (1)
  29. macbook (1)
  30. marketing (1)
  31. open-source (1)
  32. oracle (2)
  33. python (6)
  34. rails (2)
  35. ruby (1)
  36. silverlight (1)
  37. skitch (1)
  38. startups (4)
  39. tech (21)
  40. twitter (1)
  41. usability (1)
  42. web20 (6)
  43. work (3)
  44. yui (2)
ten1000miles.com | Aussie Blogs |  Feed

Creative Commons License This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.