如何显示名称而不是id

如何显示名称而不是id

本文介绍了raw_id_fields:如何显示名称而不是id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自定义Django管理控制台,我使用的是



或者,您可以显示unicode和指向值的相应管理员更改页面的链接:


Customizing a Django Admin panel, I'm using raw_id_fields to select a ForeignKey from a Model which has thousands of elements, because the default select-box drop-down is inconvenient with so many elements.

It works but it shows the id as can be seen on this image:

Is there any way to show the name or other field instead of the id? Or, is there any better way to accomplish this than using raw_id_fields?

Thanks!

****Update****

This is my code in models.py:

class Structure(MPTTModel):
    name = models.CharField(max_length=200, unique=True, verbose_name = _('name'))
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', verbose_name = _('parent'))

    def __unicode__(self):
        return u"%s" % (self.name)

In admin.py:

class StructureAdmin(tree_editor.TreeEditor):
    search_fields = ('name',)
    raw_id_fields = ('parent',)
解决方案

I've also researched this and I don't think it's possible. I think the best you can do is display the unicode of the field next to the raw id field: http://djangosnippets.org/snippets/2108/

Alternatively, you can display the unicode and a link to the value's corresponding admin change page: http://djangosnippets.org/snippets/2217/

这篇关于raw_id_fields:如何显示名称而不是id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 17:32