
上QQ阅读APP看书,第一时间看更新
Creating a model mixin with URL-related methods
For every model that is appropriate to detail on its own distinct page, it is a good practice to define the get_absolute_url() method. This method can be used in templates, and also in the Django admin site, to preview the saved object. However, get_absolute_url() is ambiguous, as it returns the URL path instead of the full URL.
In this recipe, we will look at how to create a model mixin that provides simplified support for model-specific URLs. This mixin will:
- Allow you to define either the URL path or the full URL in your model
- Generate the other of these automatically based on the one you define
- Define the get_absolute_url() method behind the scenes