mirror of https://github.com/django/django.git
Refs #18974 -- Deprecated @models.permalink() decorator.
This commit is contained in:
parent
aa9569fce1
commit
0083a4c8e9
|
@ -1,5 +1,3 @@
|
||||||
from functools import wraps
|
|
||||||
|
|
||||||
from django.core.exceptions import ObjectDoesNotExist # NOQA
|
from django.core.exceptions import ObjectDoesNotExist # NOQA
|
||||||
from django.db.models import signals # NOQA
|
from django.db.models import signals # NOQA
|
||||||
from django.db.models.aggregates import * # NOQA
|
from django.db.models.aggregates import * # NOQA
|
||||||
|
@ -37,7 +35,17 @@ def permalink(func):
|
||||||
(viewname, viewargs)
|
(viewname, viewargs)
|
||||||
(viewname, viewargs, viewkwargs)
|
(viewname, viewargs, viewkwargs)
|
||||||
"""
|
"""
|
||||||
|
import warnings
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils.deprecation import RemovedInDjango21Warning
|
||||||
|
|
||||||
|
warnings.warn(
|
||||||
|
'permalink() is deprecated in favor of calling django.urls.reverse() '
|
||||||
|
'in the decorated method.',
|
||||||
|
RemovedInDjango21Warning
|
||||||
|
)
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def inner(*args, **kwargs):
|
def inner(*args, **kwargs):
|
||||||
|
|
|
@ -41,6 +41,8 @@ details on these changes.
|
||||||
* The ``authenticate()`` method of authentication backends will require a
|
* The ``authenticate()`` method of authentication backends will require a
|
||||||
``request`` argument.
|
``request`` argument.
|
||||||
|
|
||||||
|
* The ``django.db.models.permalink()`` decorator will be removed.
|
||||||
|
|
||||||
.. _deprecation-removed-in-2.0:
|
.. _deprecation-removed-in-2.0:
|
||||||
|
|
||||||
2.0
|
2.0
|
||||||
|
|
|
@ -554,6 +554,31 @@ Miscellaneous
|
||||||
Features deprecated in 1.11
|
Features deprecated in 1.11
|
||||||
===========================
|
===========================
|
||||||
|
|
||||||
|
``models.permalink()`` decorator
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
Use :func:`django.urls.reverse` instead. For example::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
class MyModel(models.Model):
|
||||||
|
...
|
||||||
|
|
||||||
|
@models.permalink
|
||||||
|
def url(self):
|
||||||
|
return ('guitarist_detail', [self.slug])
|
||||||
|
|
||||||
|
becomes::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
class MyModel(models.Model):
|
||||||
|
...
|
||||||
|
|
||||||
|
def url(self):
|
||||||
|
return reverse('guitarist_detail', args=[self.slug])
|
||||||
|
|
||||||
Miscellaneous
|
Miscellaneous
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
|
import warnings
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils.deprecation import RemovedInDjango21Warning
|
||||||
|
|
||||||
|
|
||||||
def set_attr(name, value):
|
def set_attr(name, value):
|
||||||
|
@ -8,17 +11,20 @@ def set_attr(name, value):
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
class Guitarist(models.Model):
|
with warnings.catch_warnings():
|
||||||
name = models.CharField(max_length=50)
|
warnings.simplefilter('ignore', category=RemovedInDjango21Warning)
|
||||||
slug = models.CharField(max_length=50)
|
|
||||||
|
|
||||||
@models.permalink
|
class Guitarist(models.Model):
|
||||||
def url(self):
|
name = models.CharField(max_length=50)
|
||||||
"Returns the URL for this guitarist."
|
slug = models.CharField(max_length=50)
|
||||||
return ('guitarist_detail', [self.slug])
|
|
||||||
|
|
||||||
@models.permalink
|
@models.permalink
|
||||||
@set_attr('attribute', 'value')
|
def url(self):
|
||||||
def url_with_attribute(self):
|
"Returns the URL for this guitarist."
|
||||||
"Returns the URL for this guitarist and holds an attribute"
|
return ('guitarist_detail', [self.slug])
|
||||||
return ('guitarist_detail', [self.slug])
|
|
||||||
|
@models.permalink
|
||||||
|
@set_attr('attribute', 'value')
|
||||||
|
def url_with_attribute(self):
|
||||||
|
"Returns the URL for this guitarist and holds an attribute"
|
||||||
|
return ('guitarist_detail', [self.slug])
|
||||||
|
|
Loading…
Reference in New Issue