Django import export Line number: 1 - uColumn id not found

2024/10/7 2:24:38

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 Falseclass Meta:model = VinCasesAndCampaignswidgets = {}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)
Answer

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.

https://en.xdnf.cn/q/70295.html

Related Q&A

Why cant I access builtins if I use a custom dict as a functions globals?

I have a dict subclass like this:class MyDict(dict):def __getitem__(self, name):return globals()[name]This class can be used with eval and exec without issues:>>> eval(bytearray, MyDict()) <…

How to enable autocomplete (IntelliSense) for python package modules?

This question is not about Pygame, Im usin Pygame as an example.While experimenting with Pygame Ive noticed that autocomplete is not working for some modules. For example, if I start typing pygame.mixe…

Integrating a redirection-included method of payment in django-oscar

I am developing a shopping website using django-oscar framework, in fact I am using their sandbox site. I want to add payment to the checkout process, but the thing is, I am totally confused!Ive read t…

How can I unpack sequence?

Why cant I do this:d = [x for x in range(7)] a, b, c, d, e, f, g = *dWhere is it possible to unpack? Only between parentheses of a function?

Can I statically link Cython modules into an executable which embeds python?

I currently have an executable compiled from C++ that embeds python. The embedded executable runs a python script which load several Cython modules. Both the Cython modules and the executable are lin…

How to Store Graphs?

Lets say i have a class Graph defined.graph iowa = {.....nodes:{edges.......}}and similar graph clusters.once the script is running its all in the object. But how do you store the Graph info (including…

regex for only numbers in string?

I cant find the regex for strings containing only whitespaces or integers. The string is an input from user on keyboard. It can contain everything but \n (but it doesnt matter I guess), but we can focu…

How to build a chi-square distribution table

I would like to generate a chi-square distribution table in python as a function of the probability level and degree of freedom.How to calculate the probability, given a known chi-value and degree of f…

Reset all weights of Keras model

I would like to be able to reset the weights of my entire Keras model so that I do not have to compile it again. Compiling the model is currently the main bottleneck of my code. Here is an example of w…

How to fix NaN or infinity issue for sparse matrix in python?

Im totally new to python. Ive used some code found online and I tried to work on it. So Im creating a text-document-matrix and I want to add some extra features before training a logistic regression mo…