• <bdo id='PVYzb'></bdo><ul id='PVYzb'></ul>

        <i id='PVYzb'><tr id='PVYzb'><dt id='PVYzb'><q id='PVYzb'><span id='PVYzb'><b id='PVYzb'><form id='PVYzb'><ins id='PVYzb'></ins><ul id='PVYzb'></ul><sub id='PVYzb'></sub></form><legend id='PVYzb'></legend><bdo id='PVYzb'><pre id='PVYzb'><center id='PVYzb'></center></pre></bdo></b><th id='PVYzb'></th></span></q></dt></tr></i><div id='PVYzb'><tfoot id='PVYzb'></tfoot><dl id='PVYzb'><fieldset id='PVYzb'></fieldset></dl></div>
        <tfoot id='PVYzb'></tfoot>

        <small id='PVYzb'></small><noframes id='PVYzb'>

        <legend id='PVYzb'><style id='PVYzb'><dir id='PVYzb'><q id='PVYzb'></q></dir></style></legend>

        在 Django admin 中将外键列显示为详细对象的链接

        时间:2023-11-08
        <legend id='mSn1K'><style id='mSn1K'><dir id='mSn1K'><q id='mSn1K'></q></dir></style></legend>

        1. <i id='mSn1K'><tr id='mSn1K'><dt id='mSn1K'><q id='mSn1K'><span id='mSn1K'><b id='mSn1K'><form id='mSn1K'><ins id='mSn1K'></ins><ul id='mSn1K'></ul><sub id='mSn1K'></sub></form><legend id='mSn1K'></legend><bdo id='mSn1K'><pre id='mSn1K'><center id='mSn1K'></center></pre></bdo></b><th id='mSn1K'></th></span></q></dt></tr></i><div id='mSn1K'><tfoot id='mSn1K'></tfoot><dl id='mSn1K'><fieldset id='mSn1K'></fieldset></dl></div>
            <tbody id='mSn1K'></tbody>
              <bdo id='mSn1K'></bdo><ul id='mSn1K'></ul>
              <tfoot id='mSn1K'></tfoot>

              <small id='mSn1K'></small><noframes id='mSn1K'>

                • 本文介绍了在 Django admin 中将外键列显示为详细对象的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如 link-in-django-admin-to- 中所述foreign-key-object,可以将 ForeignKey 字段显示为管理详细信息页面的链接.

                  As explained in link-in-django-admin-to-foreign-key-object, one can display a ForeignKey field as a link to the admin detail page.

                  总结一下,

                  class Foo(Model):
                      bar = models.ForeignKey(Bar)
                  
                  class FooAdmin(ModelAdmin):
                      list_display = ('link_to_bar',)
                      def link_to_bar(self, obj):
                          link = urlresolvers.reverse('admin:app_bar_change', args=[obj.bar_id])
                          return u'<a href="%s">%s</a>' % (link, obj.bar) if obj.bar else None
                      link_to_bar.allow_tags = True
                  

                  问题是:我们可以更自动地做到这一点吗?例如,向 FooAdmin 定义提供一个外键列表,以显示为详细页面的链接:

                  The question is: can we do it more automatically? For instance, provide to the FooAdmin definition a list of foreign key to display as links to detail page:

                  class FooAdmin(ModelAdmin):
                      ...
                      list_foreign_key_links = ('bar',)
                      ...
                  

                  我知道这些 ModelAdmin 类是通过元类编程生成的.那么,应该是可以的.这样做是一个好的开始?

                  I know that these ModelAdmin classes are generated with metaclass programming. Then, it should be possible. What would be a good start to do so?

                  推荐答案

                  下面的解决方案使用这个答案但使其可被所有模型重用,避免了向每个管理类添加方法的需要.

                  The solution below uses this answer but makes it reusable by all models, avoiding the need to add methods to each admin class.

                  # models.py
                  from django.db import models
                  
                  class Country(models.Model):
                      name = models.CharField(max_length=200)
                      population = models.IntegerField()
                  
                  class Career(models.Model):
                      name = models.CharField(max_length=200)
                      average_salary = models.IntegerField()
                  
                  class Person(models.Model):
                      name = models.CharField(max_length=200)
                      age = models.IntegerField()
                      country = models.ForeignKey(Country, on_delete=models.CASCADE)
                      career = models.ForeignKey(Career, on_delete=models.CASCADE)
                  

                  示例管理员

                  # admin.py
                  from django.utils.html import format_html
                  from django.urls import reverse
                  
                  from .models import Person
                  
                  
                  def linkify(field_name):
                      """
                      Converts a foreign key value into clickable links.
                      
                      If field_name is 'parent', link text will be str(obj.parent)
                      Link will be admin url for the admin url for obj.parent.id:change
                      """
                      def _linkify(obj):
                          linked_obj = getattr(obj, field_name)
                          if linked_obj is None:
                              return '-'
                          app_label = linked_obj._meta.app_label
                          model_name = linked_obj._meta.model_name
                          view_name = f'admin:{app_label}_{model_name}_change'
                          link_url = reverse(view_name, args=[linked_obj.pk])
                          return format_html('<a href="{}">{}</a>', link_url, linked_obj)
                  
                      _linkify.short_description = field_name  # Sets column name
                      return _linkify
                  
                  
                  
                  @admin.register(Person)
                  class PersonAdmin(admin.ModelAdmin):
                      list_display = [
                          "name",
                          "age",
                          linkify(field_name="country"),
                          linkify(field_name="career"),
                      ]
                  


                  结果

                  给定一个名为 app 的 App,以及一个 Person 实例 Person(name='Adam' age=20),其国家和职业外键值以及 ids 123456,列表结果将是:


                  Results

                  Given an App named app, and a Person instance Person(name='Adam' age=20) with country and carreer foreign key values with ids 123 and 456, the list result will be:

                  | Name | Age |                          Country                          |...|
                  |------|-----|-----------------------------------------------------------|...|
                  | Adam |  20 | <a href="/admin/app/country/123">Country object(123)</a>  |...|
                  

                  (继续)

                  |...|                          Career                         |
                  |---|---------------------------------------------------------|
                  |...| <a href="/admin/app/career/456">Career object(456)</a>  |
                  

                  这篇关于在 Django admin 中将外键列显示为详细对象的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Django 1.6 应用程序中实现降价? 下一篇:Django - 如何从模型实例中获取管理员 URL

                  相关文章

                    <i id='SUFX7'><tr id='SUFX7'><dt id='SUFX7'><q id='SUFX7'><span id='SUFX7'><b id='SUFX7'><form id='SUFX7'><ins id='SUFX7'></ins><ul id='SUFX7'></ul><sub id='SUFX7'></sub></form><legend id='SUFX7'></legend><bdo id='SUFX7'><pre id='SUFX7'><center id='SUFX7'></center></pre></bdo></b><th id='SUFX7'></th></span></q></dt></tr></i><div id='SUFX7'><tfoot id='SUFX7'></tfoot><dl id='SUFX7'><fieldset id='SUFX7'></fieldset></dl></div>

                    <small id='SUFX7'></small><noframes id='SUFX7'>

                  1. <tfoot id='SUFX7'></tfoot>
                    • <bdo id='SUFX7'></bdo><ul id='SUFX7'></ul>

                    1. <legend id='SUFX7'><style id='SUFX7'><dir id='SUFX7'><q id='SUFX7'></q></dir></style></legend>