Comments (5)
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.
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.
Thank you very much for the explanation and workaround.
from django-import-export.
np - I'll add a fix for this when I get a chance.
from django-import-export.
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)
- 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
- Why always assume the model going have id as primary key? why can not we dynamically get PK field from model? HOT 11
- 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 5
- How to explicitly disable and enable bulk import? HOT 1
- Widget configuration documentation is unclear
- Related model with primary key causes import exception (regression)
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.