Giter VIP home page Giter VIP logo

Comments (11)

matthewhegarty avatar matthewhegarty commented on May 30, 2024 1

Thanks for raising this. Can you try with this PR and see if it solves your issue?

from django-import-export.

BabuDip avatar BabuDip commented on May 30, 2024

@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.

matthewhegarty avatar matthewhegarty commented on May 30, 2024

Please can you test if this PR fixes your import issue?

from django-import-export.

BabuDip avatar BabuDip commented on May 30, 2024

@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.

matthewhegarty avatar matthewhegarty commented on May 30, 2024

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.

BabuDip avatar BabuDip commented on May 30, 2024

@matthewhegarty,

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

from django-import-export.

matthewhegarty avatar matthewhegarty commented on May 30, 2024

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.

BabuDip avatar BabuDip commented on May 30, 2024

Hi @matthewhegarty,

Sure. Here is the walkthrough:
Once the server (with above config) is ready I added a site with below details:
image

To test the basic workflow, just exported the data as is:
image

which looks something like below:
image

and when I try to import the same file, it prints the below error (with import_error_display = ("message", "row", "traceback")):
image

Please let me know if you need any further information.

Thank you.

from django-import-export.

matthewhegarty avatar matthewhegarty commented on May 30, 2024

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.

matthewhegarty avatar matthewhegarty commented on May 30, 2024

Closing - please update if you have any more information, and I can re-open if it is a library issue.

from django-import-export.

BabuDip avatar BabuDip commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.