Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- If the code in the urlman docs is run with the below Urls class:
- .. code-block:: python
- class Group(models.Model):
- ...
- class urls(Urls):
- view = "/{self.slug}/"
- users = "{view}users/"
- admin = "{view}admin/"
- This model will also have a ``get_absolute_url`` method also:
- .. code-block:: pycon
- >>> group = Group(slug="hello")
- >>> group.get_absolute_url()
- '/hello/'
- >>>> group.urls.view
- '/hello/'
- """
- import urlman
- class UrlsMeta(urlman.UrlsMetaclass):
- def __set_name__(self, cls, name, **kwargs):
- urls = self.urls
- def get_absolute_url(self):
- return self.urls.view
- if not hasattr(cls, 'get_absolute_url') and 'view' in urls:
- setattr(cls, 'get_absolute_url', get_absolute_url)
- class Urls(urlman.Urls, metaclass=UrlsMeta):
- """
- urlman.Urls that adds a get_absolute_url method automatically
- """
Add Comment
Please, Sign In to add comment