Django import export如何读取导入此文件

时间:2020-06-13 14:15:41

标签: django django-import-export

这是要导入的导入文件的图片

x

我想阅读课程列,但是当我排除组列时,它显示所有列均被跳过。如何导入该文件?

这是当前代码:

class courseAttendanceResource(resources.ModelResource):
Student_ID = Field(attribute='Student_ID', column_name='Student ID')
Username = Field(attribute='Username', column_name='Username')
ID_number = Field(attribute='ID_number', column_name='ID number')
Institution = Field(attribute='Institution', column_name='Institution')
Department = Field(attribute='Department', column_name='Department')
Surname = Field(attribute='Surname', column_name='Surname')
First_name = Field(attribute='First_name', column_name='First name')
Groups = Field(attribute='Groups', column_name='Groups')
P = Field(attribute='P', column_name='P')
L = Field(attribute='L', column_name='L')
E = Field(attribute='E', column_name='E')
A = Field(attribute='A', column_name='A')
Taken_sessions = Field(attribute='Taken_sessions', column_name='Taken sessions')
Points = Field(attribute='Points', column_name='Points')
Percentage = Field(attribute='Percentage', column_name='Percentage')

def get_export_headers(self):
    headers = super().get_export_headers()
    for i, h in enumerate(headers):
        if h == 'Student ID':
            headers[i] = 'Student_ID'
        if h == 'Username':
            headers[i] = 'Username'
        if h == 'ID number':
            headers[i] = 'ID_number'
        if h == 'Institution':
            headers[i] = 'Institution'
        if h == 'Department':
            headers[i] = 'Department'
        if h == 'Surname':
            headers[i] = 'Surname'
        if h == 'First name':
            headers[i] = 'First_name'
        if h == 'Groups':
            headers[i] = 'Groups'
        if h == 'P':
            headers[i] = 'P'
        if h == 'L':
            headers[i] = 'L'
        if h == 'E':
            headers[i] = 'E'
        if h == 'A':
            headers[i] = 'A'
        if h == 'Taken sessions':
            headers[i] = 'Taken_sessions'
        if h == 'Points':
            headers[i] = 'Points'
        if h == 'Percentage':
            headers[i] = 'Percentage'
    return headers

class Meta:
    model = courseAttendance

    with open('/tmp/rows.csv') as csvfile:
        # skip the first 3 lines
        [next(csvfile, None) for i in range(3)]

        # now load the dataset
        ds = tablib.Dataset()
        ds.csv = csvfile.read()
        print(ds)
        dataset = ds
        result = resource.import_data(dataset)
    import_id_fields = ('Student_ID',)
    exclude = ('Course', 'Group',)
    fields = ('Student_ID', 'Username', 'ID_number', 'Institution', 'Department', 'Surname', 'First_name', 'Groups',
              'P', 'L', 'E', 'A', 'Taken_sessions', 'Points', 'Percentage',)
    #export_order = ('Student_ID', 'Username', 'ID_number', 'Institution', 'Department', 'Surname','First_name', 'Groups', 'P', 'L', 'E', 'A','Taken_sessions', 'Points', 'Percentage')
    skip_unchanged = True
    report_skipped = True

我需要阅读本课程,并排除“组”列。之后,我想阅读其余的专栏。我应用了您的答案,但给我一个错误

result = resource.import_data(dataset)

1 个答案:

答案 0 :(得分:1)

在致电import_data()之前创建一个Dataset

给出一个csv文件:

Course
Group

Student ID,Username
123,abc
import tablib

with open('/tmp/rows.csv') as csvfile:
    # skip the first 3 lines
    [next(csvfile, None) for i in range(3)]

    # now load the dataset
    ds = tablib.Dataset()
    ds.csv = csvfile.read()
    print(ds)

产生:

Student ID|Username
----------|--------
123       |abc     

一旦加载了数据集,就可以调用import_data()方法:

result = resource.import_data(dataset)
相关问题