简体   繁体   中英

Django import-export of excel data

I'm using django import export (DIE) to import some data. I have a problem importing the data.

my 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)

I want to import an excel file like:

在此处输入图片说明

But:

在此处输入图片说明

I want to import all the rows . Please tell me how to ignore the duplicates. Thanks in advance!

change line

skip_unchanged = True

to

skip_unchanged = False

OK I find one way to import. Import the file with save(). If you guys have a better way please tell me~

The code as follow:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM