5

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?

Aleyna
  • 1,857
  • 4
  • 20
  • 27

1 Answers1

12

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.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • 1
    Excerpt from django doc: "This decorator(permalink) takes the *name* of a URL pattern (either a view name or a URL pattern name) and a list of position or keyword arguments and uses the URLconf patterns to construct the correct, full URL." How come using permalink hurts DRY? We still feed @permalink with a url name or view path without having to re-declaring it? – Aleyna Apr 20 '13 at 04:41
  • You're absolutely right - I wrote the answer without properly reading the documentation. I'll do some research and see if I can change my answer. – Josh Smeaton Apr 20 '13 at 04:51
  • Thanks Josh. I was not able to find this and I believe the doc did not mention that it is deprecated. – Aleyna Apr 20 '13 at 16:04
  • @Aleyna they decided *not* to deprecate it because it would break the upgrade path for no great reason. They may deprecate in version 2, but they're preparing the way with the warning in the docs. – Josh Smeaton Apr 21 '13 at 03:52
  • @permalink is now deprecated, starting from django 1.11. See https://docs.djangoproject.com/en/1.11/releases/1.11/#models-permalink-decorator – Antwane Apr 06 '17 at 07:32