raw_id_fields: How to show a name instead of id?

1,577 views
Skip to first unread message

Almudena Vila Forcén

unread,
Mar 5, 2013, 4:45:26 AM3/5/13
to django...@googlegroups.com

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:

 enter image description here

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?

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',)
Thanks in advance.

विश्वासराव साळुंखे

unread,
Aug 7, 2015, 8:46:56 AM8/7/15
to Django users

Hi,
Try Grappelli's Autocomplete lookups functionality.

Thanks,
Vishwas
Reply all
Reply to author
Forward
0 new messages