我有一个Person模型,它与Book有外键关系,Book有许多字段,但我最关心的是author(一个标准CharField)。

话虽如此,在我的PersonAdmin模型中,我想显示book。作者使用list_display:

class PersonAdmin(admin.ModelAdmin):
    list_display = ['book.author',]

我已经尝试了所有显而易见的方法,但似乎都不起作用。

有什么建议吗?


当前回答

和其他人一样,我也选择了可调用对象。但它们有一个缺点:默认情况下,你不能在上面点餐。幸运的是,有一个解决方案:

姜戈 >= 1.8

def author(self, obj):
    return obj.book.author
author.admin_order_field  = 'book__author'

Django < 1.8

def author(self):
    return self.book.author
author.admin_order_field  = 'book__author'

其他回答

我刚刚发布了一个片段,使管理。ModelAdmin支持'__'语法:

http://djangosnippets.org/snippets/2887/

所以你可以这样做:

class PersonAdmin(RelatedFieldAdmin):
    list_display = ['book__author',]

这基本上只是做与其他回答中描述的相同的事情,但它自动负责(1)设置admin_order_field(2)设置short_description和(3)修改queryset以避免对每一行进行数据库命中。

根据文档,你只能显示ForeignKey的__unicode__表示:

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#list-display

似乎奇怪的是,它不支持'book__author'风格的格式,这种格式在DB API中随处可见。

事实证明,这个功能有一个门票,它被标记为不会修复。

如果你有很多关系属性字段要在list_display中使用,并且不想为每个字段创建一个函数(及其属性),一个肮脏但简单的解决方案将覆盖ModelAdmin实例__getattr__方法,在运行中创建可调用对象:

class DynamicLookupMixin(object):
    '''
    a mixin to add dynamic callable attributes like 'book__author' which
    return a function that return the instance.book.author value
    '''

    def __getattr__(self, attr):
        if ('__' in attr
            and not attr.startswith('_')
            and not attr.endswith('_boolean')
            and not attr.endswith('_short_description')):

            def dyn_lookup(instance):
                # traverse all __ lookups
                return reduce(lambda parent, child: getattr(parent, child),
                              attr.split('__'),
                              instance)

            # get admin_order_field, boolean and short_description
            dyn_lookup.admin_order_field = attr
            dyn_lookup.boolean = getattr(self, '{}_boolean'.format(attr), False)
            dyn_lookup.short_description = getattr(
                self, '{}_short_description'.format(attr),
                attr.replace('_', ' ').capitalize())

            return dyn_lookup

        # not dynamic lookup, default behaviour
        return self.__getattribute__(attr)


# use examples    

@admin.register(models.Person)
class PersonAdmin(admin.ModelAdmin, DynamicLookupMixin):
    list_display = ['book__author', 'book__publisher__name',
                    'book__publisher__country']

    # custom short description
    book__publisher__country_short_description = 'Publisher Country'


@admin.register(models.Product)
class ProductAdmin(admin.ModelAdmin, DynamicLookupMixin):
    list_display = ('name', 'category__is_new')

    # to show as boolean field
    category__is_new_boolean = True

这里的要点是

可调用的特殊属性如boolean和short_description必须定义为ModelAdmin属性,例如book__author_verbose_name = '作者名'和category__is_new_boolean = True。

可调用的admin_order_field属性是自动定义的。

不要忘记在ModelAdmin中使用list_select_related属性,以使Django避免额外的查询。

尽管上面有很多很棒的答案,但由于我是Django的新手,我仍然被困住了。以下是我从新手的角度给出的解释。

models.py

class Author(models.Model):
    name = models.CharField(max_length=255)

class Book(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=255)

admin.py(不正确的方式)-你认为它可以通过使用'model__field'引用来工作,但事实并非如此

class BookAdmin(admin.ModelAdmin):
    model = Book
    list_display = ['title', 'author__name', ]

admin.site.register(Book, BookAdmin)

admin.py(正确的方式)——这是你在Django中引用外键名的方式

class BookAdmin(admin.ModelAdmin):
    model = Book
    list_display = ['title', 'get_name', ]

    def get_name(self, obj):
        return obj.author.name
    get_name.admin_order_field  = 'author'  #Allows column order sorting
    get_name.short_description = 'Author Name'  #Renames column head

    #Filtering on side - for some reason, this works
    #list_filter = ['title', 'author__name']

admin.site.register(Book, BookAdmin)

如需更多参考,请参阅Django模型链接

作为另一种选择,你可以像这样查找:

#models.py
class UserAdmin(admin.ModelAdmin):
    list_display = (..., 'get_author')
    
    def get_author(self, obj):
        return obj.book.author
    get_author.short_description = 'Author'
    get_author.admin_order_field = 'book__author'

从Django 3.2开始,你可以使用display()装饰器:

#models.py
class UserAdmin(admin.ModelAdmin):
    list_display = (..., 'get_author')
    
    @admin.display(ordering='book__author', description='Author')
    def get_author(self, obj):
        return obj.book.author