Django管理列表视图在自定义列

时间:2017-11-20 09:30:02

标签: django django-admin

我们有一个列表视图,如下所示 enter image description here

现在假设我们想在视图中添加一个在模型中不存在的列。所以我们可以通过在管理类中创建一个方法来实现。

class PageAdmin(admin.ModelAdmin):

    list_display = ["id", "title", "slug", "author", 'updated', 'custom_field']

    def custom_field(self, obj):
        # logic...
        return True # or False based on above logic

这很简单。 现在让我们采取一个更复杂的场景,我需要根据goodreads.com搜索的结果填充此列。 我的意思是我将在goodreads上搜索对象title并找到确切的标题,如果找到则返回True否则为False。

例如,有一个页面对象,其标题是饥饿游戏,所以当我搜索这个时,我会得到https://www.goodreads.com/search?query=hunger+games

我将解析结果并检查前10个结果中是否存在确切的标题。

class PageAdmin(admin.ModelAdmin):

    list_display = ["id", "title", "slug", "author", 'updated', 'custom_field']

    def custom_field(self, obj):
        # search on goodreads.com, parse results and match with obj.title
        return True # or False based on above logic

这种方法的问题在于它使管理页面非常慢..因为它将获取页面中所有对象的数据(通常是100个对象)

我该如何改进?是否有任何异步方式来做到这一点,而不会改变太多的html / css / js。 如何在管理类中使用 changelist_view 方法作为我的用例

PS:如果它是一个不同的域,我也想考虑批量搜索方法。我的意思是代替点击搜索api 100次,我只会打一次并传递所有对象的标题(以逗号分隔)。 https://api.goodreads.com/search?query=title1,title2,title3 ...

假设这只会返回完全匹配的书籍,并且根据ID,我可以在管理页面上显示True或False

PPS:我无法在db表中添加另一列,如果外部服务停机或响应时间过长,我会显示一些相关消息。

1 个答案:

答案 0 :(得分:1)

class OrderAdmin(admin.ModelAdmin):

    list_display = ["id", "title", "slug", "author", 'updated', 'custom_field']

    def custom_field(self, obj):
        return obj.title in self.found_in_goodreads

    def get_paginator(self, *args, **kwargs):
        paginator = super().get_paginator(*args, **kwargs)
        request, queryset, count = args
        page = int(request.GET.get('p', 1))  #  For current page only
        titles = paginator.page(page).object_list.values_list('title', flat=True)

        # search_goodreads
        # url = "https://api.goodreads.com/search?query={}".format(''.join(titles))
        # response = requests.get(url)
        # parse the response and store titles in the list
        # self.found_in_goodreads = [title for titles in response]
        return paginator

此外,您必须处理goodreads服务器关闭,返回400,超时等情况。

相关问题