如何集成Django Import Export和Wagtail?

时间:2019-09-24 19:54:08

标签: django django-admin wagtail django-import-export

我正在尝试将django-import-export中的模型资源添加到Wagtail的管理员中。我能找到的唯一文档说您将通过钩子来完成。问题是,我不断收到错误消息:

missing 2 required positional arguments: 'model' and 'admin_site'

整个resourceModelAdmin是:

class AccountResource(resources.ModelResource):

    class Meta:
        model = Account
        fields = ('first_name', 'last_name', 'email', 'created', 'archived')

class AccountsAdmin(ImportExportModelAdmin, ModelAdmin):
    resource_class = AccountResource
    model = Account
    menu_label = 'Accounts'  # ditch this to use verbose_name_plural from model
    menu_icon = 'group'  # change as required
    menu_order = 200  # will put in 3rd place (000 being 1st, 100 2nd)
    add_to_settings_menu = False  # or True to add your model to the Settings sub-menu
    exclude_from_explorer = False # or True to exclude pages of this type from Wagtail's explorer view
    list_display = ('first_name', 'last_name', 'email', 'created', 'archived')
    search_fields = ('first_name', 'last_name', 'email', 'created')

# Now you just need to register your customised ModelAdmin class with Wagtail
modeladmin_register(AccountsAdmin)

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

Watail ModelAdmin不共享Django ModelAdmin的API。

django-import-export中的mixins预计将与Django ModelAdmin一起使用,并且由于您的经验而无法与Wagtail ModelAdmin一起使用。

对于导出功能,我通过将Django export_action的{​​{1}}和ModelAdmin钩到Wa ExportMixin的URL中来解决了这个问题。

这可能不太漂亮,但是可以重用ModelAdmin中的视图和逻辑。

我已经发布了一个使用这种设计的example Project on GitHub

here所能找到的实际胶合代码并不多:

ExportMixin

我认为可以执行类似的操作来实现导入部分。