Comments (11)
Thanks for raising this. Can you try with this PR and see if it solves your issue?
from django-import-export.
@matthewhegarty
Thanks for the swift response. I have tested it and it works for export with custom pk.
I am still having import issues, sorry I should have split it into another issue.
from django-import-export.
Please can you test if this PR fixes your import issue?
from django-import-export.
@matthewhegarty I have tested this PR and it does not work in my case, Trying to import data with custom PK field.
from django-import-export.
Do you have import_id_fields = "id"
defined in your resource? Is it possible you could create a new issue with steps to reproduce?
from django-import-export.
Below is the steps to reproduce:
requirements.txt
django-import-export==4.0.1
django==5.0.6
openpyxl==3.1.2
models.py
from django.db import models
class Division(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Divisions"
class Site(models.Model):
division = models.ForeignKey(Division, on_delete=models.CASCADE)
code = models.CharField(max_length=10, primary_key=True)
name = models.CharField(max_length=100)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "Sites"
admin.py
from django.contrib import admin
from import_export import resources
from import_export.admin import ImportExportMixin
from .models import Division, Site
class DivisionResource(resources.ModelResource):
class Meta:
model = Division
fields = ("id", "name", "description", "created_at", "updated_at")
@admin.register(Division)
class DivisionAdmin(ImportExportMixin, admin.ModelAdmin):
resource_class = DivisionResource
list_display = ("id", "name", "description", "created_at", "updated_at")
search_fields = ("name", "description")
class SiteResource(resources.ModelResource):
class Meta:
model = Site
fields = ("division", "code", "name", "description", "created_at", "updated_at")
import_id_fields = ("code",)
@admin.register(Site)
class SiteAdmin(ImportExportMixin, admin.ModelAdmin):
resource_class = SiteResource
list_display = (
"division",
"code",
"name",
"description",
"created_at",
"updated_at",
)
search_fields = ("code", "name", "description")
Everything works fine, except importing site (which has pk field as code)
from django-import-export.
Can you add this to SiteAdmin
and try again to see a full stack trace:
import_error_display = ("message", "row", "traceback")
Can you post an example row of your import file.
from django-import-export.
Hi @matthewhegarty,
Sure. Here is the walkthrough:
Once the server (with above config) is ready I added a site with below details:
To test the basic workflow, just exported the data as is:
which looks something like below:
and when I try to import the same file, it prints the below error (with import_error_display = ("message", "row", "traceback")):
Please let me know if you need any further information.
Thank you.
from django-import-export.
I can't see what is causing this. You have the following declaration:
class SiteResource(resources.ModelResource):
class Meta:
model = Site
fields = ("division", "code", "name", "description", "created_at", "updated_at")
import_id_fields = ("code",)
Which clearly defines the import_id_fields
, meaning that id
should not be used. Are you definitely using v4.0.1? If you set a breakpoint here can you verify that 'code' is being returned as the import_id_field
?
Try declaring the following in SiteAdmin
to rule out any issues there.
resource_classes = [SiteResource]
Can you step through with a debugger and isolate what is happening?
from django-import-export.
Closing - please update if you have any more information, and I can re-open if it is a library issue.
from django-import-export.
Hi @matthewhegarty, with version 4.0.1, it gets below:
self._meta.import_id_fields = ['id']
however, with recent version 4.0.3, it is now correct:
self._meta.import_id_fields = ('code',)
Thank you. All resolved now.
from django-import-export.
Related Issues (20)
- Can't import file without an `id` column in V4 HOT 4
- v4 add custom delimiter for csv. HOT 5
- Referencing a column alias in the `fields` list causes a crash on import HOT 1
- `use_natural_foreign_keys` in ModelResource.Meta not working in V4 HOT 2
- TypeError: Widget.__init__() got an unexpected keyword argument 'key_is_id' when upgrading from 3.8 to 4.0 HOT 15
- Lost all the default import and export options after upgrading to V4 HOT 2
- Default field set differs during import in v4 HOT 3
- `resource_class` was deprecated and removed, but it still works HOT 2
- Export crashes if the column name is used instead of the attribute name
- Resource field is empty when importing data HOT 5
- Fixed Arabic translate for 'Import' word HOT 2
- Breaking change due to _check_import_id_fields HOT 2
- Improve parsing of DateTimeWidget HOT 4
- `TypeError: Widget.__init__() got an unexpected keyword argument 'allow_blank'` HOT 4
- numeric fields exported as text by default since v4 to excel HOT 4
- Customize export format depending on file type
- ModelResource field order in admin import page HOT 5
- Exporting fields with changed `column_name` doesn't work in v4 HOT 9
- How to explicitly disable and enable bulk import? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-import-export.