Django admin Foreignkey ManyToMany list_display展示

代码语言:javascript
复制
class Ghost(models.Model):
    create = models.DateTimeField(default=timezone.now, help_text='创建时间')
    update = models.DateTimeField(auto_now=True, help_text='修改时间')
    speed = models.IntegerField(default=0, help_text='速度')
    name = models.CharField(max_length=64, help_text='名称')


class InstanceTask(models.Model):
    create = models.DateTimeField(default=timezone.now, help_text='创建时间')
    update = models.DateTimeField(auto_now=True, help_text='修改时间')
    name = models.CharField(max_length=64, help_text='副本名称')

class InstanceTaskMap(models.Model):
    create = models.DateTimeField(default=timezone.now, help_text='创建时间')
    update = models.DateTimeField(auto_now=True, help_text='修改时间')
    name = models.CharField(max_length=64, help_text='地图名称')
    ghosts = models.ManyToManyField(Ghost, help_text='Ghost')
    instance_task = models.ForeignKey(InstanceTask, related_name='instancetask_instancetaskmap', blank=True, null=True,
                                      help_text='副本任务', on_delete=models.SET_NULL)

对于上面的model,如果要在django admin中展示ghosts信息,那么在list_display中直接加入’ghosts’ 会报下面的错误:The value of ‘list_display1’ must not be a ManyToManyField.

如果要解决这个问题可以使用下面的代码来展示:

代码语言:javascript
复制
class InstanceTaskMapAdmin(admin.ModelAdmin):
list_display = ('name', 'instance_task', 'id', 'index', 'get_ghost_name', 'introduction')

# https://blog.csdn.net/weixin_42134789/article/details/83686664
def get_ghost_name(self, obj):
    ghost_list = []
    for g in obj.ghosts.all():
        ghost_list.append(g.ghost.name)
    return ','.join(ghost_list)

get_ghost_name.short_description = &#34;Ghosts&#34; </code></pre></div></div><p>如果需要更丰富的信息可以参考上面代码注释中的链接。</p><p>对于foreignkey同样可以使用这样的方式进行反向查询展示所有相关的model。</p><p>例如要在InstanceTask页面展示所有的InstanceTaskMap,可以使用下面的代码:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">class InstanceTaskAdmin(admin.ModelAdmin):
list_display = (&#39;name&#39;, &#39;id&#39;, &#39;need_level&#39;, &#39;times_limit&#39;, &#39;instance_type&#39;,&#39;get_map_list&#39;, &#39;introduction&#39;)

def get_map_list(self, obj):
    map_list = []
    for g in obj.instancetask_instancetaskmap.all().order_by(&#39;index&#39;):
        map_list.append(g.name + &#39;(&#39; + str(g.index) + &#39;)&#39;)
    return &#39;,&#39;.join(map_list)

get_map_list.short_description = &#34;Maps&#34;  </code></pre></div></div><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:100%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1723238301669998472.jpg" /></div></div></div></figure><p>  ☆文章版权声明☆ </p><p>* 网站名称:<strong>obaby@mars</strong></p><p> * 网址:https://h4ck.org.cn/</p><p> * 本文标题: 《Django admin Foreignkey ManyToMany list_display展示》</p><p> * 本文链接:https://h4ck.org.cn/2019/12/django-admin-foreignkey-manytomanykey-list_display%e5%b1%95%e7%a4%ba/</p><p> * 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。</p><figure class=""><hr/></figure><p><strong>分享文章:</strong></p><h4 id="155657" name="%E7%9B%B8%E5%85%B3%E6%96%87%E7%AB%A0:">相关文章:</h4><ol class="ol-level-0"><li>Django REST framework foreignkey 序列化 </li><li>django raw_id_fields 显示名称而不是id(raw_id_fields: How to show a name instead of id) </li><li>Django 限制访问频率 </li><li>Apache2 Django {“detail”:”Authentication credentials were not provided.”} </li><li>wp-admin 诡异的404和500错误 </li><li>再谈《Django 限制访问频率》 </li><li>Django input value值被截断 </li><li>Django APScheduler + uwsgi 定时任务重复运行 </li><li>django 主动抛出 403 异常 </li><li>ngix+uwsgi+django 以及阿里云rds数据库数据导入 </li></ol>