xadmin importexport插件使用过程出现的问题记录

1、要安装django-import-export版本,并将此应用注册到

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apps.devices.apps.DevicesConfig',
    'xadmin',
    'crispy_forms',
    'envs',
    'assets',
    'meeting',
    'terminals',
    'import_export',
]

2、使用的tablib to 0.12.1版本,否则会出现导入csv时报错‘Error encountered while trying to read file: test.csv’

Problem
Error encountered while trying to read file CSV with python 2.7 #957

Solution
Downgrade tablib to 0.12.1

Acceptance Criteria
Doc updated with good dependencies

 

3、导入时报错:    "This is forbidden when an 'atomic' block is active."
TransactionManagementError: This is forbidden when an 'atomic' block is active.

原因分析:详见https://github.com/django-import-export/django-import-export/issues/609

This is because import_export.resource.Resource.import_data() tests for connection.features.supports_transaction. This is OK on most RDBMS, but on MySQL the support for transactions depends on the database engine being used, so Django runs a test to see if the DB supports transactions. During the test, a temporary table is created and also calls set_autocommit(False). That call can only be executed outside of an atomic block (Django code test for in_atomic_block for the DB connection).

This also has the side effect the table created by Django (called ROLLBACK_TEST) to test transaction support is never deleted from the DB, because the rollback to the savepoint marked by Django is never performed.

I'll submit a Pull Request with a short patch to prevent this: if the import_data() is running inside an atomic block, then we know the DB supports transactions and we don't test for connection.features.supports_transaction.

解决办法:

import_export/resources.py 的

supports_transactions = getattr(connection.features, "supports_transactions", False)

改为:
supports_transactions = getattr(connection, 'in_atomic_block', False) or getattr(connection.features, "supports_transactions", False)

4、为了控制在导入预览页面展示的内容条数,增加以下优化:

当导入数据量行数很大时,预览所有数据意义不大。
(1)找到路径:xadmin\plugins\importexport(xadmin包源码)
大概在156行处,可以找到ImportView视图;
再往下,在234行处:

result = resource.import_data(dataset, dry_run=True,
                              raise_errors=False,
                              file_name=import_file.name,
                              user=request.user)
context['result'] = result
后面增加:
if len(result.rows) < 20:
    result_display = result.rows
else:
    result_display = result.rows[:20]
context['result_display'] = result_display

context[‘result_display’]就是通过TemplateResponse传给模板展示预览数据。
(2)修改模板
找到路径:templates/xadmin/import_export/import.html(xadmin包源码)
在大约99行位置,
将{% for row in result.rows %}改成{% for row in result_display %}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Django Xadmin 的列表视图中添加自定义按钮,可以使用 Xadmin 的 `ButtonPlugin` 插件。 以下是添加自定义按钮的步骤: 1. 创建一个 `ButtonPlugin` 子类,例如: ```python from xadmin.plugins.utils import get_context_dict from xadmin.views import BaseAdminPlugin, ListAdminView class MyButtonPlugin(BaseAdminPlugin): # 定义按钮的图标和名称 my_btn_icon = 'fa fa-plus' my_btn_name = 'My Button' # 在页面右上角添加按钮 def get_context(self, __): context = get_context_dict(__) context['my_button_html'] = self.get_my_button_html() return context # 构建按钮的 HTML def get_my_button_html(self): return '<a class="btn btn-primary" href="#">' \ '<i class="{}"></i> {}</a>'.format(self.my_btn_icon, self.my_btn_name) ``` 2. 将 `ButtonPlugin` 子类添加到 `ListAdminView` 中: ```python class MyListAdminView(ListAdminView): # ... # 注册自定义按钮插件 plugin_my_button = MyButtonPlugin # ... ``` 3. 在模板中添加按钮的 HTML: ```html {% extends "xadmin/base_site.html" %} {% load static %} {% block content %} {{ block.super }} <div class="row-fluid"> <div class="span12"> <div class="widget-box"> <div class="widget-title"> <h5>{{ title }} List</h5> <div class="buttons"> {{ my_button_html }} <!-- 添加自定义按钮的 HTML --> </div> </div> <div class="widget-content nopadding"> <!-- ... --> </div> </div> </div> </div> {% endblock %} ``` 这样,在列表视图页面的右上角就会出现一个名为 My Button 的自定义按钮。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值