Search code examples
pythondjangocsvdjango-import-export

Django-import-export importing empty values always


Greetins im using Django-import-export with django 3, im trying to import a csv file but its always importing like there is no data in the file.

i have followed the tutorial, and try diferent approach in the configuration of the resource, but nothing works, it always upload all the data empty like this: empty data

this is my model:

class Product (Master):

    internal_id = models.IntegerField(verbose_name=_("internal id"), default=0)
    year = models.IntegerField(verbose_name=_("model year"), default=0)
    engine = models.CharField(verbose_name=_("engine type"), max_length=100, null=True, blank=True)
    fuel = models.CharField(verbose_name=_("fuel type"), max_length=100,null=True, blank=True)
    gearbox = models.CharField(verbose_name=_("gearbox type"), max_length=100, null=True, blank=True)
    traction = models.CharField(verbose_name=_("traction"), max_length=100, null=True, blank=True)
    color = models.CharField(verbose_name=_("Color"), max_length=100, null=True, blank=True)
    doors = models.IntegerField(verbose_name=_("doors number"), default=4, null=True, blank=True)
    seats = models.IntegerField(verbose_name=_("seats number"), default=5, null=True, blank=True)
    length = models.FloatField(verbose_name=_("length"), default=0, null=True, blank=True)
    height = models.FloatField(verbose_name=_("height"), default=0, null=True, blank=True)
    wide = models.FloatField(verbose_name=_("wide"), default=0, null=True, blank=True)
    mileage = models.FloatField(verbose_name=_("mileage"),default=0, null=True, blank=True)

    cylinders = models.IntegerField(verbose_name=_("cylinders"),default=0, null=True, blank=True)
    brand = models.CharField(verbose_name=_("brand"), max_length=100, null=True, blank=True)

    price = models.BigIntegerField(verbose_name=_("price"), default=0, null=True, blank=True)
    category = models.ForeignKey('products.Category', on_delete=models.CASCADE, blank=True, null=True, verbose_name=_("category"))

    url=models.SlugField(max_length=100, verbose_name=_("url"))


    class Meta:
        ordering = ["internal_id", "name", "updated"]
        verbose_name = _("product")
        verbose_name_plural = _("products")

    def save(self, *args, **kwargs):
        url=str(self.internal_id)+str(random.randrange(0,1000))+"-"+str(self.name)
        self.url = slugify(url)
        super(Product, self).save(*args, **kwargs)

this is my resource:

class ProductResource(resources.ModelResource):

    class Meta:
        model = Product
        fields = ['id', 'internal_id', 'category', 'brand', 'name', 'year', 'fuel', 'color', 'gearbox', 'traction', 'cylinders', 'mileage', 'price']

this is my admin:

@admin.register(Product)
class ProductsAdmin(ImportExportModelAdmin):
    resource_class = ProductResource
    list_display = ('id', 'internal_id', 'name', 'active', 'url', 'created', 'updated')
    fieldsets = [
                    (_('main details'),         {'fields': ['internal_id','name','year','brand','price','engine','gearbox','category']}),
                    (_('secondary details'),    {'fields': ['fuel','traction','mileage','cylinders','seats','doors','color','description']}),
                    (_('dimensions'),           {'fields': ['height', 'length', 'wide']}),
                    (_('others'),               {'fields': ['url','active'],'classes': ['collapse', 'extrapretty']}),
                ]

    inlines = [ProductImageInline]

and this is my data

,1,1,Nissan,SENTRA B17,2014,G,GRIS,AUTOMATICO,4X2,1800,75,567, 27.500.000 
,2,1,Nissan,SENTRA B17,2014,G,GRIS,AUTOMATICO,4X2,1800, 70.455 , 27.500.000 
,3,1,Nissan,SENTRA B17,2014,G,GRIS,AUTOMATICO,4X2,1800,91.904, 26.500.000 
,4,1,Nissan,SENTRA B17,2014,G,GRIS,AUTOMATICO,4X2,1800,85,327, 27.000.000 
,5,2,Nissan,NEW PATHFINDER MM8,2014,G,GRIS,AUTOMATICO,4X4,3500,68,038, 58.000.000 
,6,2,Nissan,QASHQAI J10,2014,G,GRIS,AUTOMATICO,4X2,2000,93,969, 33.000.000 
,7,2,Nissan,NEW X-TRAIL EXCLUSIVE T32,2015,G,GRIS OSCURO,AUTOMATICO,4X4,2500,129,816, 47.500.000 
,8,2,Nissan,NEW X-TRAIL EXCLUSIVE T32,2015,G,GRIS OSCURO,AUTOMATICO,4X4,2500,97,699, 50.000.000 
,9,2,Nissan,NEW X-TRAIL EXCLUSIVE T32,2015,G,GRIS,AUTOMATICO,4X4,2500,111,968, 48.500.000 
,10,2,Nissan,NEW X-TRAIL EXCLUSIVE T32,2015,G,GRIS,AUTOMATICO,4X4,2500,80,526, 51.000.000 
,11,2,Nissan,NEW X-TRAIL EXCLUSIVE T32,2015,G,GRIS,AUTOMATICO,4X4,2500,83,331, 51.000.000 
,12,2,Nissan,NEW X-TRAIL EXCLUSIVE T32,2015,G,GRIS OSCURO,AUTOMATICO,4X4,2500,116,980, 48.000.000 
,13,3,Nissan,NAVARA D40,2015,D,GRIS,AUTOMATICO,4X4,2500,109,157, 48.000.000 
,14,3,Nissan,NAVARA D40,2015,D,GRIS,AUTOMATICO,4X4,2500,48,589, 53.000.000 
,15,3,Nissan,FRONTIER D23,2016,D,GRIS,MANUAL,4X4,2500,107,572, 45.000.000 
,16,3,Nissan,FRONTIER D23,2016,D,GRIS,MANUAL,4X4,2500,108,873, 45.000.000 
,17,3,Nissan,FRONTIER D23,2016,D,BLANCO,MANUAL,4X4,2500,110,230, 45.000.000 

its there something i'm doing wrong? thanks a lot for your help


Solution

  • I'd suggest adding some data to the Product via the admin. After that try to export them to csv file and next to import the data from the csv file. P.S. And I guess each column in csv file must have a header.

    All the best.