Django import export Line number: 1 - u\"Column 'id' not found

我与影子孤独终老i 提交于 2019-12-04 07:26:15

Here is a working example. I also used "exclude" and it works and no need to add exclude = ('id',) in models.py. My data set does not contain "id" field.

Python 2.7 | Django 1.11 | Django Import export 1.0.0 (2018-02-13)

Models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User

# Create your models here.

class CourseList(models.Model):
    short_code = models.CharField(max_length=10)
    course_id = models.CharField(max_length=50,null=True,blank=True,unique=True)
    title = models.CharField(max_length=100)
    status = models.CharField(max_length=10)
    count_current = models.IntegerField(default=0)
    count_cumulative = models.IntegerField(default=0)
    start_date = models.CharField(max_length=20)
    end_date = models.CharField(max_length=20)
    pacing_type = models.CharField(max_length=10)
    updated_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['end_date']

        def __unicode__(self):
            return self.title

resources.py

from import_export import resources, fields
from .models import CourseList

class CourseListResource(resources.ModelResource):
    status = fields.Field(column_name='availability', attribute="status")
    short_code = fields.Field(column_name='catalog_course', attribute="short_code")
    title = fields.Field(column_name='catalog_course_title', attribute="title")
    count_current = fields.Field(column_name='count', attribute="count_current")
    count_cumulative = fields.Field(column_name='cumulative_count', attribute="count_cumulative")


    class Meta:
        model = CourseList
        import_id_fields = ('course_id',)
        exclude = ('id', 'updated_date',)
        skip_unchanged = True
        fields = ('status', 'short_code','course_id', 'title', 'count_current', 'count_cumulative', 'end_date', 'pacing_type', 'start_date', )

admin.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin

# Register your models here.
from import_export import resources
from courselist.models import CourseList
from courselist.resources import CourseListResource


@admin.register(CourseList)
class CourseListAdmin(ImportExportModelAdmin):
    resource_class = CourseListResource
    list_display = ('id', 'course_id', 'title', 'start_date', 'end_date')

include field id in the field of ModelResource

fields = ('id','VIN','LatestOpenCaseID','LatestClosedCaseID', 
              'OpenDate', 'CloseDate', 'HasCampaigns',)

and in your excel file add a column with filed id with empty values. this add a new field with autoincrement.

file.xls

id  VIN  OpenDate ...
    23   05-10-2018
    24   05-11-2018
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!