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

一世执手 提交于 2019-12-06 02:49:45

问题


I am trying to import excel documents into a Django DB. I have added the following code to admin.py and model.py. There seems to be an error in the development of Django. I have read through several different documentations about how to fix this error. But I am still a little lost on how to implement it exactly.

In the Trace it keeps saying that my excel document needs an id field. There is no id field in my excel docs nor did I tell my model to look for an id field.

The documentation that I have found states that I should use get_or_init_instance here:

https://django-import-export.readthedocs.org/en/latest/import_workflow.html

Any help that you guys could give would be great.

admin.py

class VinCasesAndCampaignsResource(resources.ModelResource):
    published = fields.Field(column_name='published_date')

    def get_instance(self, instance_loaders, row):
        return False

    class Meta:
        model = VinCasesAndCampaigns
        widgets = {}
        fields = ('VIN','LatestOpenCaseID','LatestClosedCaseID', 
                  'OpenDate', 'CloseDate', 'HasCampaigns',)
        import_id_fields = ['VIN']
        export_order = ('VIN',)
        exclude = ('id')

model.py

class VinCasesAndCampaigns(models.Model):
    VIN = models.CharField(max_length=30)
    LatestOpenCaseID = models.DateField()
    LatestClosedCaseID = models.DateField()
    OpenDate = models.DateField()
    CloseDate = models.DateField()
    HasCampaigns = models.BooleanField(default = False)
    HasOpenCampaigns = models.BooleanField(default = False)
    HasCases = models.BooleanField(default = False)
    HasEstimates = models.BooleanField(default = False)
    HasDwell = models.BooleanField(default = False)
    HasClaims = models.BooleanField(default = False)

    exclude = ('id',)

Trace:

> Line number: 1 - u"Column 'id' not found in dataset. Available columns
> are: [u'VIN', u'LatestOpenCaseID', u'LatestClosedCaseID', u'OpenDate',
> u'CloseDate', u'HasCampaigns', u'HasOpenCampaigns', u'HasCases',
> u'HasEstimates', u'HasDwell', u'HasClaims']" Traceback (most recent
> call last): File
> "/Users/USER/anaconda/lib/python2.7/site-packages/django_import_export-0.2.8.dev0-py2.7.egg/import_export/resources.py",
> line 342, in import_data instance, new =
> self.get_or_init_instance(instance_loader, row)

回答1:


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



回答2:


Just create an empty column namely id before and then import the same file via Django import_export.

Make sure id column should be of your first column in the CSC or Excel file




回答3:


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


来源:https://stackoverflow.com/questions/30512150/django-import-export-line-number-1-ucolumn-id-not-found

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