mary
mary

Reputation: 11

Django Import Export : How to append rows before import

I am trying to import csv file in django model import export but the end results doesn't show any data and shows "skipped" in the preview. file is having 2 additional rows as in row 1 and 2.I want to exclude those 2 rows.I want to read data from row 3. Please Help in this regard.

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
        import_id_fields = ('Student_ID',)
        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


class courseAttendanceAdmin(ImportExportModelAdmin):
    resource_class =courseAttendanceResource

Upvotes: 0

Views: 824

Answers (1)

Matthew Hegarty
Matthew Hegarty

Reputation: 4306

If you have enabled skip_unchanged, then that can explain why the import process is skipping rows. It will be because the logic has decided that your import rows are identical to existing rows and therefore should be skipped. See skip_row for more information.

If the existing skip_row logic doesn't work for you, you can override this method and maybe custom logic here will help you to skip the first two rows and process row 3.

There are a number of factors here, but you haven't given us much to go on in your question. Please refer to this resource for help with posting questions.

Upvotes: 0

Related Questions