Django import export Line number: 1 - u"Column 

2020-07-11 06:03发布

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)

4条回答
淡お忘
2楼-- · 2020-07-11 06:36

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

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

查看更多
手持菜刀,她持情操
3楼-- · 2020-07-11 06:42

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')
查看更多
手持菜刀,她持情操
4楼-- · 2020-07-11 06:43

You can solve this problem by using the following steps:

Step 1 first goto C:/Users/am.sa18/Desktop/myenv/Lib/site-packages/import_export/resources.py

Step 2 open resources.py in the editor.

Step 3 do change in line no 84, its looks like this

Step 4 import_id_fields = ['id'] Where 'id' by-default set by import_export package, you can change 'id' by your primary key of the model. After adding primary key : import_id_fields = ['primary_key']

Step 5 Save resources.py file and run the server.

查看更多
干净又极端
5楼-- · 2020-07-11 06:44

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
查看更多
登录 后发表回答