Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免每次在页面上调用get_absolute_url(django)?_Django_Django Models_Django Templates_Django Views - Fatal编程技术网

如何避免每次在页面上调用get_absolute_url(django)?

如何避免每次在页面上调用get_absolute_url(django)?,django,django-models,django-templates,django-views,Django,Django Models,Django Templates,Django Views,每个人都说在模板中使用get_absolute_url是非常好的实践。但在我的例子中,它会在单个页面上对数据库进行大量相同的查询。 下面是我必须开发的URL结构(我不能更改它,因为客户已经有了工作网站,如果我更改URL,Google不会喜欢它)-mysite/category/subcategory/product_slug.html 以下是url模式的代码: from django.conf.urls import url 从。导入视图 urlpatterns = [ url(r'^

每个人都说在模板中使用get_absolute_url是非常好的实践。但在我的例子中,它会在单个页面上对数据库进行大量相同的查询。 下面是我必须开发的URL结构(我不能更改它,因为客户已经有了工作网站,如果我更改URL,Google不会喜欢它)-mysite/category/subcategory/product_slug.html 以下是url模式的代码:

from django.conf.urls import url
从。导入视图

urlpatterns = [
    url(r'^(?P<parent_category_slug>[\w-]+)/(?P<category_slug>[\w-]+)/(?P<slug>[\w-]+)\.html$', views.ProductDetailView.as_view(), name='product_detail'),
    url(r'^(?P<parent_slug>[\w-]+)/(?P<slug>[\w-]+)$', views.ProductListView.as_view(), name='products_of_category'),
    url(r'^(?P<slug>\w+)$', views.SubCategoryListView.as_view(), name='sub_category'),
    url(r'^$', views.CatalogIndexListView.as_view(), name='index'),
]
所以,当我进入mysite/category/subcategory时,我看到所有的产品都属于这个子类别。这是一个列表(实际上是表格,包含图像、标题等)。 所有图片和标题都必须是指向产品的URL。 下面是模板中的一段代码

e {% for product in products %}

                    <tr>
                        <td class="product_name_list">
                            <a href="{{ product.get_absolute_url }}">{{ product.product_name }}</a>
                        </td>
                        <td class="product_article_list">{{ product.product_article }}</td>
                        {% if product.product_main_image  %}
                            <td class="product_image_list"><a href="{{ product.get_absolute_url }}" ><img src='{{ product.product_main_image.url}}' alt=""></a></td>
                        {% else %}
                            <td class="product_image_list"><a href="{{ product.get_absolute_url }}" ><img src='{% static "images/empty.gif" %}' alt=""></a></td>
                        {% endif %}

                        <td class="product_request_list"><a href="#">Запросить</a></td>
                    </tr>

             {% endfor %}
e{%用于产品中的产品%}
{{product.product_article}}
{%if product.product_main_image%}
{%else%}
{%endif%}
{%endfor%}
所以,结果是,我对数据库进行了大量查询,因为get_absolute_url被反复调用

请帮我避免这个。我曾尝试使用“get_related()”设置默认管理器类,但这很愚蠢,显然没有帮助,因为每个实例都会一次又一次地调用方法get_absolute_url

提前谢谢

您可以使用django的decorator来解决这个问题

from django.utils.functional import cached_property

# You can either use it convert `get_abolute_url` method to property
@cached_property
def get_absolute_url(self):
    return reverse(
        'product_detail', kwargs={
            'slug':self.slug,
            'parent_category_slug':self.product_category.category_parent.slug,
            'category_slug':self.product_category.slug})

# or decorate the method with different name so that you can use both

cached_absolute_url = cached_property(get_absolute_url)
这样你就可以同时使用

object.get_absolute_url()

object.cached_absolute_url

cached\u属性
缓存方法的值,这样当您再次调用它时,它将直接返回缓存的值,而不是运行整个方法。

我无法在您的答案中添加“+”。你的回答不是我真正需要的,但仍然很有用。我明白了,我应该在我的视图中的queryset中使用{%url%},并使用“选择相关”、“预回迁相关”。使用url标记与使用
获取绝对url
相同。事实上,通常建议使用
get_absolute\u url
over
url
标记来生成模型对象的干链接。在
get_absolute\u url
上使用
@cached\u属性
装饰器有哪些缺点?我很惊讶默认情况下文档中不推荐它?我不理解用例中是否有单独的
get\u absolute\u url
一个可能的缺点是,如果
get\u absolute\u url
的值由于模型实例中的某些更改而更改,那么缓存的url仍将指向过时的url
cached\u absolute\u url
只是一个示例,它将获取url的实际方式和缓存方式分开。
object.get_absolute_url()

object.cached_absolute_url