0

我正在使用django import export(DIE) 导入一些数据。我在导入数据时遇到问题。

我的admin.py

class TestResource(resources.ModelResource):

    class Meta:
            model = Test
            exclude = ('id',)
            import_id_fields = ['VS',]
            skip_unchanged = True

 class TestAdmin(ImportExportMixin,admin.ModelAdmin):

      fieldsets = [
        ('VS',      {'fields':['VS']}),
        ('pool',    {'fields':['pool']}),
        ('pool_port', {'fields':['pool_port']}),
        ('idc',        {'fields':['idc']})
       ]

    list_display = ('VS','pool','pool_port','idc')
    list_filter = ['pool_port']
    search_fields = ['VS','pool','pool_port','idc']
    resource_class = TestResource
admin.site.register(Test,TestAdmin)

我想导入一个excel文件,例如:

在此处输入图像描述

但:

在此处输入图像描述

我想导入所有行。请告诉我如何忽略重复项。提前致谢!

4

2 回答 2

1

换线

skip_unchanged = True

skip_unchanged = False
于 2018-03-28T07:33:02.190 回答
0

好的,我找到了一种导入方式。使用 save() 导入文件。如果大家有更好的方法请告诉我~

代码如下:

import os 
os.environ.setdefault("DJANGO_SETTINGS_MODULE","mysite.settings")

import django
django.setup()

import xlrd
from Test.models import Test


def input(VS,pool_text,port,idc_text):
    i = Test(
        VS = vs,
        pool = pool_text,
        pool_port = port,
        idc = idc_text
        )
    i.save()

files = xlrd.open_workbook('test.xls')
sh = files.sheet_by_index(0)
n = 0
for i in range(1,sh.nrows):

    vs = sh.cell(i,0).value
    pool_text = sh.cell(i,1).value
    port = sh.cell(i,2).value
    idc = sh.cell(i,3).value
    input(vs,pool_text,port,idc)
    n += 1
print n
于 2017-06-06T11:51:04.273 回答