Reusing Django Changelist Outside of Admin Site

后端 未结 1 1594
悲&欢浪女
悲&欢浪女 2020-12-29 09:47

The Django changelist table is really cool - searchable, filterable, multi-select actions etc.

I\'m building a custom backend for an app and I keep realizing: this i

相关标签:
1条回答
  • 2020-12-29 10:24

    ChangeList as a class is really cool and feature-full. However, it's hard to use outside the context of the AdminSite monolith.

    The ChangeList class takes 12 required __init__() parameters. That number alone should steer you away and doubly so when you realize those are all sourced from the Admin changelist_view(). While those parameters have remained the same since Django 1.1, they did change from 1.0 and it's so much a Django internal object, I wouldn't rely on its interface being stable.

    The best way to use ChangeList — or specifically to get the changelist benefits (which is what you are after) — is to use the changelist_view() method. Using that of course requires using/subclassing AdminSite. This is worth doing, or at least trying out. Looks like you already are.

    That method takes the request parameter and likes /(?P<app_label>%s)/(?P<model_name>%s)/ in the URL route that points to it.

    Digging into the code:

    • ChangeList lives in django.contrib.admin.views.main
    • changelist_view() is a method on django.contrib.admin.options.ModelAdmin

    UPDATE: In Django 1.4, both ChangeList and changelist_view() changed by adding one and two new parameters respectively.

    0 讨论(0)
提交回复
热议问题