如何在django admin change_list模板中获取该对象

时间:2021-01-21 22:56:42

I want to override the django admin change_list page but i am not able to find how to get the actual object so that i can access the proerties like object.name

我想覆盖django admin change_list页面,但我无法找到如何获取实际对象,以便我可以访问像object.name这样的属性

In the template they have this code

在模板中,他们有这个代码

<tr >{% for item in result %}{{ item }}{% endfor %}</tr>

{%for result in item%} {{item}} {%endfor%}

where is the actual object which i can use

我可以使用的实际对象在哪里

EDIT:

编辑:

It look like result is the row and item is the column. i want something like result.pk

它看起来像是行,而item是列。我想要像result.pk这样的东西

This yield the list if results

如果结果,这将产生列表

https://github.com/django/django/blob/master/django/contrib/admin/templatetags/admin_list.py#L175

https://github.com/django/django/blob/master/django/contrib/admin/templatetags/admin_list.py#L175

1 个解决方案

#1


12  

The context fed to change_list.html includes a cl entry corresponding to a contrib.admin.views.main.ChangeList object, which is the object containing the result list.

提供给change_list.html的上下文包括对应于contrib.admin.views.main.ChangeList对象的cl条目,该对象是包含结果列表的对象。

You can access the result list directly like this:

您可以像这样直接访问结果列表:

{% for object in cl.result_list %}
{{ object.field }}
{% endfor %}

The change list results are rendered as part of the change_list_results.html template through use of the result_list template tag. The change_list_results.html template also has the cl context variable present when rendering the template.

通过使用result_list模板标记,更改列表结果将作为change_list_results.html模板的一部分呈现。 change_list_results.html模板在呈现模板时也具有cl上下文变量。

When the Django template is iterating over result in your example, result is a ResultList object which contains pre-rendered html, the underlying object which is being rendered is not made available.

当Django模板在您的示例中迭代结果时,result是一个ResultList对象,其中包含预呈现的html,正在呈现的基础对象不可用。

To override the template at this level, it's likely you'll need to implement your own result_list type template tag which can return a list of results with the underlying objects attached as an attribute to each result.

要覆盖此级别的模板,您可能需要实现自己的result_list类型模板标记,该标记可以返回结果列表,其中基础对象作为属性附加到每个结果。

In short it is likely that you will need to:

简而言之,您可能需要:

  • Create your own result_list template tag, based on the Django's implementation. Rather than have it return a results context as a list of ResultList prerendered html, have it return results containing objects which are both capable of being rendered to html, as well as each item having the original underlying object attached for later use in your template.
  • 根据Django的实现创建自己的result_list模板标记。让它返回结果上下文作为ResultList预呈现的html列表,让它返回包含能够呈现为html的对象的结果,以及附加原始底层对象的每个项目,以便以后在模板中使用。
  • Override the change_list.html template to use your new tag rather than Django's result_list template tag.
  • 覆盖change_list.html模板以使用新标记而不是Django的result_list模板标记。
  • Override the change_list_results.html template to make use of the extra information available from your template tag such as the presence of each underlying object.
  • 覆盖change_list_results.html模板以利用模板标记中提供的额外信息,例如每个底层对象的存在。

As you've probably gathered, the admin app is quite tightly integrated through the layers. Changing its operation is non-trivial and requires changes and overrides in multiple parts of the source.

正如您可能已经收集的那样,管理员应用程序通过各层紧密集成。更改其操作并非易事,需要更改和覆盖源的多个部分。

#1


12  

The context fed to change_list.html includes a cl entry corresponding to a contrib.admin.views.main.ChangeList object, which is the object containing the result list.

提供给change_list.html的上下文包括对应于contrib.admin.views.main.ChangeList对象的cl条目,该对象是包含结果列表的对象。

You can access the result list directly like this:

您可以像这样直接访问结果列表:

{% for object in cl.result_list %}
{{ object.field }}
{% endfor %}

The change list results are rendered as part of the change_list_results.html template through use of the result_list template tag. The change_list_results.html template also has the cl context variable present when rendering the template.

通过使用result_list模板标记,更改列表结果将作为change_list_results.html模板的一部分呈现。 change_list_results.html模板在呈现模板时也具有cl上下文变量。

When the Django template is iterating over result in your example, result is a ResultList object which contains pre-rendered html, the underlying object which is being rendered is not made available.

当Django模板在您的示例中迭代结果时,result是一个ResultList对象,其中包含预呈现的html,正在呈现的基础对象不可用。

To override the template at this level, it's likely you'll need to implement your own result_list type template tag which can return a list of results with the underlying objects attached as an attribute to each result.

要覆盖此级别的模板,您可能需要实现自己的result_list类型模板标记,该标记可以返回结果列表,其中基础对象作为属性附加到每个结果。

In short it is likely that you will need to:

简而言之,您可能需要:

  • Create your own result_list template tag, based on the Django's implementation. Rather than have it return a results context as a list of ResultList prerendered html, have it return results containing objects which are both capable of being rendered to html, as well as each item having the original underlying object attached for later use in your template.
  • 根据Django的实现创建自己的result_list模板标记。让它返回结果上下文作为ResultList预呈现的html列表,让它返回包含能够呈现为html的对象的结果,以及附加原始底层对象的每个项目,以便以后在模板中使用。
  • Override the change_list.html template to use your new tag rather than Django's result_list template tag.
  • 覆盖change_list.html模板以使用新标记而不是Django的result_list模板标记。
  • Override the change_list_results.html template to make use of the extra information available from your template tag such as the presence of each underlying object.
  • 覆盖change_list_results.html模板以利用模板标记中提供的额外信息,例如每个底层对象的存在。

As you've probably gathered, the admin app is quite tightly integrated through the layers. Changing its operation is non-trivial and requires changes and overrides in multiple parts of the source.

正如您可能已经收集的那样,管理员应用程序通过各层紧密集成。更改其操作并非易事,需要更改和覆盖源的多个部分。