Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the permalink decorator no longer recommended in django?

I was reading the django dev documentation. Here it says permalink decorator is no longer recommended, use reverse inside your get_absolute_url method to generate full url for a model instance(scroll a little above and check out the warning box).

I think it is violating DRY that we have to use reverse each and every time we need it. So what's wrong with using permalink? Why is it no longer recommended?

like image 467
Aleyna Avatar asked Apr 20 '13 04:04

Aleyna


1 Answers

In current versions of django, the decorator literally calls the reverse function that the documentation recommends anyway. The reason seems to be that the decorator is unnecessary now that we have reverse(). Using reverse does look nicer than returning a name, a tuple, and a dictionary. Instead, you use args and kwargs - idiomatic python.

And this is the ticket that discussed deprecating the decorator for API purity sake. Instead of raising warnings and making users update code bases, they decided to simply put a warning in the docs.

The permalink decorator should be deprecated and ultimately removed. It was introduced to solve the problem of having to hardcode urls into get_absolute_url. However it violates one of the major rules of good decorators in that in forces the function signature to change in order to deal with the fact it's been decorated. Additionally it does not provide any useful functionality over using reverse() directly within the body of get_absolute_url.

like image 57
Josh Smeaton Avatar answered Nov 15 '22 05:11

Josh Smeaton