Giter VIP home page Giter VIP logo

Comments (5)

ipamo avatar ipamo commented on May 29, 2024

Traceback

Traceback (most recent call last):
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 785, in import_row
diff = self.get_diff_class()(self, original, new)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 259, in __init__
self.left = self._export_resource_fields(resource, instance)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 280, in _export_resource_fields
return [
^
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 281, in <listcomp>
resource.export_field(f, instance) if instance else ""
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "…\import-db-default\.venv\Lib\site-packages\import_export\resources.py", line 1066, in export_field
return field.export(obj)
^^^^^^^^^^^^^^^^^
File "…\import-db-default\.venv\Lib\site-packages\import_export\fields.py", line 148, in export
return self.widget.render(value, obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "…\import-db-default\.venv\Lib\site-packages\import_export\widgets.py", line 266, in render
value = timezone.localtime(value)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "…\import-db-default\.venv\Lib\site-packages\django\utils\timezone.py", line 182, in localtime
if is_naive(value):
^^^^^^^^^^^^^^^
File "…\import-db-default\.venv\Lib\site-packages\django\utils\timezone.py", line 234, in is_naive
return value.utcoffset() is None
^^^^^^^^^^^^^^^
AttributeError: 'DatabaseDefault' object has no attribute 'utcoffset'

from django-import-export.

matthewhegarty avatar matthewhegarty commented on May 29, 2024

Thanks for raising. I have found that you can reproduce the same issue in the shell with:

>>> from django.db.models.functions.datetime import Now
>>> from django.utils import timezone
>>> timezone.localtime(Now())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/matthew/.virtualenvs/django-import-export/lib/python3.11/site-packages/django/utils/timezone.py", line 182, in localtime
    if is_naive(value):
       ^^^^^^^^^^^^^^^
  File "/Users/matthew/.virtualenvs/django-import-export/lib/python3.11/site-packages/django/utils/timezone.py", line 234, in is_naive
    return value.utcoffset() is None
           ^^^^^^^^^^^^^^^

The interface of timezone.localtime() states that a datetime instance is passed, therefore it's not valid to pass a Now instance.

The issue is that the 'diff' process used to show the differences in the import uses DateTimeWidget which is being asked to render a Now instance (it assumes it is being passed a date). Hence it crashes. This is only occurring on the import of new objects, so I expect that the issue occurs when diffing against a 'new' instance.

I added a test here.

I don't know what the solution is at the moment.

As a workaround, you can avoid the crash during import if you set skip_diff to True on the resource:

class BookResource(ModelResource):
    class Meta:
        model = Book
        skip_diff = True

This means that you would not be able to see diffs on import.

from django-import-export.

ipamo avatar ipamo commented on May 29, 2024

Thank you very much for the explanation and workaround.

from django-import-export.

matthewhegarty avatar matthewhegarty commented on May 29, 2024

np - I'll add a fix for this when I get a chance.

from django-import-export.

matthewhegarty avatar matthewhegarty commented on May 29, 2024

I suggest that the best way to fix this would be for each widget render() method to have a type check which checks that the value passed corresponds to the underlying type, if not then a TypeError is raised. Callers further up the stack can then catch this error and handle accordingly.

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.