Django 管理 ListView 在自定义列中显示外部 url 数据

标签 django django-admin

我们有一个如下所示的 ListView enter image description here

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

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。

例如,有一个页面对象,它的标题是hunger games,所以当我搜索它时,我会得到 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:如果是不同的域,我还想考虑批量搜索方法。我的意思是我不会点击 search api 100 次,而是只点击一次并传递所有对象的标题(逗号分隔)。 https://api.goodreads.com/search?query=title1,title2,title3 ...

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

PPS:我无法在数据库表中添加另一列,如果外部服务出现故障或响应时间过长,我将显示一些相关消息。

最佳答案

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、超时等情况。

关于Django 管理 ListView 在自定义列中显示外部 url 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47388730/

相关文章:

python - Django/python - 消除关于日期和时区意识的混淆

python - 当我的项目(Django)发生任何变化时如何自动运行测试?

python - 带有 webmonkey 教程的 Django 中的 URL 模式问题

python - Django 管理员 TypeError : buffer object expected

django - 使用变量中的字段名称更新 django 中的多对多字段

python - Django 创建帖子

python - 学习 Django - 很好的入门项目

添加中间表单后,Django 管理站点操作不起作用

database - 在 django admin 中更改字段名称

python - Wagtail 0.8.6 - 管理员 - NoneType' 对象没有属性 'allowed_subpage_types'