raw_id_fields: How to show a name instead of id?

前端 未结 4 1591
北恋
北恋 2020-12-13 18:58

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 i

4条回答
  •  -上瘾入骨i
    2020-12-13 19:16

    It seems this plugin: https://github.com/lincolnloop/django-dynamic-raw-id

    does what you want:

    (copied from the doc):

    Usage

    To start using django-dynamic-raw-id in your application all you need to do is implement DynamicRawIDMixin in your ModelAdmin class and add the desired fields to a list of dynamic_raw_id_fields:

    from dynamic_raw_id.admin import DynamicRawIDMixin
    
    class UserProfileAdmin(DynamicRawIDMixin, admin.ModelAdmin):
        dynamic_raw_id_fields = ('user',)
    

    You can use dynamic_raw_id widgets in a Admin filter as well:

    from dynamic_raw_id.admin import DynamicRawIDMixin
    from dynamic_raw_id.filters import DynamicRawIDFilter
    
    class UserProfileAdmin(DynamicRawIDMixin, admin.ModelAdmin):
        list_filter = (
            ('dynamic_raw_id_fk', DynamicRawIDFilter),
        )
    

提交回复
热议问题