【问题标题】:Django admin list view show external url data in custom columnDjango 管理列表视图在自定义列中显示外部 url 数据
【发布时间】:2017-11-20 09:30:02
【问题描述】:

我们有一个类似下面的列表视图

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

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 表中添加另一列,如果外部服务关闭或响应时间过长,我将显示一些相关消息。

【问题讨论】:

    标签: django django-admin


    【解决方案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、超时等情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      • 2019-02-10
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多