Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xls data cell format #96

Closed
sprenge opened this issue Jun 12, 2014 · 11 comments
Closed

xls data cell format #96

sprenge opened this issue Jun 12, 2014 · 11 comments

Comments

@sprenge
Copy link

sprenge commented Jun 12, 2014

I have the impression that for xls import the data cell format must be a string. I get import failures when the cell input type is for instance numeric.

@bmihelac
Copy link
Member

Can you provide stack trace, tablib version, and minimal failing xls file.

@sprenge
Copy link
Author

sprenge commented Jun 13, 2014

I will do so later today. I should have done this in the first place, sorry for this.

@sprenge
Copy link
Author

sprenge commented Jun 15, 2014

Here is the requested info :

django-import-export==0.2.2

Error message

Line number: 1 - invalid literal for int() with base 10: ''

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/import_export/resources.py", line 318, in import_data
instance, new = self.get_or_init_instance(instance_loader, row)
File "/usr/local/lib/python2.7/dist-packages/import_export/resources.py", line 149, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
ValueError: invalid literal for int() with base 10: ''

@sprenge
Copy link
Author

sprenge commented Jun 15, 2014

failing xls file attached

On Fri, Jun 13, 2014 at 8:08 AM, Bojan Mihelac notifications@github.com
wrote:

Can you provide stack trace, tablib version, and minimal failing xls file.


Reply to this email directly or view it on GitHub
https://github.com/bmihelac/django-import-export/issues/96#issuecomment-45978786
.

@bmihelac
Copy link
Member

@sprenge can you upload xls from web interface, it does not seem to work over email.
id column is empty in xls, right, and you are using default instance loader?

@NathanQ
Copy link

NathanQ commented Jan 4, 2016

@bmihelac I'm having an issue with the format as well. Here is my model with two charfields:

class Coupon(models.Model):
    office_code = models.CharField(
        max_length=10,
        unique=True
        )
    online_code = models.CharField(
        max_length=10,
        unique=True
        )

    class Meta:
        verbose_name = 'Coupon Pair'
        verbose_name_plural = 'Coupon Pairs'

    def __unicode__(self):
        return unicode(self.id)

And, the admin:

class CouponResource(resources.ModelResource):

    class Meta:
        model = Coupon
        exclude = ('id', )
        import_id_fields = ['office_code', 'online_code', ]
        skip_unchanged = True


class CouponAdmin(ImportExportModelAdmin):

    resource_class = CouponResource

    class Meta:
        model = Coupon
        list_per_page = 20

On a test import, if a column, say office_code, only contains numbers, the importer will add a '.0' on the end of each. In the excel file, I've tried using the format tool to format as text, but this didn't help. How do I go about saving without adding the periods and zeros?

Thanks a lot for the helpful plugin!

@sassanh
Copy link

sassanh commented Aug 2, 2016

@bmihelac any updates on this? I'm getting ValueError: invalid literal for int() with base 10: '' as well:

Traceback (most recent call last):
File ".../lib/python3.5/site-packages/import_export/resources.py", line 408, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File ".../lib/python3.5/site-packages/import_export/resources.py", line 232, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File ".../lib/python3.5/site-packages/import_export/resources.py", line 226, in get_instance
return instance_loader.get_instance(row)
File ".../lib/python3.5/site-packages/import_export/instance_loaders.py", line 33, in get_instance
return self.get_queryset().get(**params)
File ".../lib/python3.5/site-packages/django/db/models/query.py", line 378, in get
clone = self.filter(*args, **kwargs)
File ".../lib/python3.5/site-packages/django/db/models/query.py", line 790, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File ".../lib/python3.5/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File ".../lib/python3.5/site-packages/django/db/models/sql/query.py", line 1243, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File ".../lib/python3.5/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File ".../lib/python3.5/site-packages/django/db/models/sql/query.py", line 1203, in build_filter
condition = self.build_lookup(lookups, col, value)
File ".../lib/python3.5/site-packages/django/db/models/sql/query.py", line 1099, in build_lookup
return final_lookup(lhs, rhs)
File ".../lib/python3.5/site-packages/django/db/models/lookups.py", line 19, in __init__
self.rhs = self.get_prep_lookup()
File ".../lib/python3.5/site-packages/django/db/models/lookups.py", line 57, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File ".../lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 744, in get_prep_lookup
return self.get_prep_value(value)
File ".../lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''

@sassanh
Copy link

sassanh commented Aug 2, 2016

It doesn't happen with xlsx, just happens with xls.

@bcanyelles
Copy link

I fixed it with overriding clean method of CharWidget:

class CharWidget(widgets.CharWidget):

    def clean(self, value, row=None, *args, **kwargs):
        if isinstance(value, float):
            value = int(value)
            return "{}".format(value)
        return value

@water4168
Copy link

water4168 commented Jan 11, 2019

i have the same issue:

Traceback (most recent call last):
File "C:\Users\water\Envs\CMS\lib\site-packages\import_export\resources.py", line 471, in import_row
self.import_obj(instance, row, dry_run)
File "C:\Users\water\Envs\CMS\lib\site-packages\import_export\resources.py", line 343, in import_obj
self.import_field(field, obj, data)
File "C:\Users\water\Envs\CMS\lib\site-packages\import_export\resources.py", line 330, in import_field
field.save(obj, data, is_m2m)
File "C:\Users\water\Envs\CMS\lib\site-packages\import_export\fields.py", line 115, in save
cleaned = self.clean(data)
File "C:\Users\water\Envs\CMS\lib\site-packages\import_export\fields.py", line 71, in clean
raise ValueError("Column '%s': %s" % (self.column_name, e))
ValueError: Column 'project': invalid literal for int() with base 10: '底层链1.0'

@andrewgy8
Copy link
Member

@walterliang Did you try the fix that was submitted by @bcanyelles ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants