2

Using: Python 3.10.4
Django 4.06
Django-import-export 2.8.0

I am trying to import data to use as demo data into my django application. I keep getting an error of localtime() cannot be applied to a naive datetime (after working around another error that I asked separately). I am not worried about this particular field being naive datetime. It's one that needs to be set manually in real application.

### models.py
class Reservation(models.Model):
    reservation = models.OneToOneField(Vehicle, on_delete=models.CASCADE, primary_key=True,)
    delivered = models.BooleanField('Delivered',default=False)
    date_reserved = models.DateTimeField('date reserved', default=datetime.datetime.now)
    ...

### admin.py
class ReservationResource(resources.ModelResource):
    class Meta:
        model = Reservation
        exclude = ('id',)
        import_id_fields = ('reservation',)
        fields = (
            'reservation',
            'delivered',
            'date_reserved',
            ...
        )
class ReservationImportExport(ImportExportModelAdmin):
    resource_class: ReservationResource
@admin.register(Reservation)
class ReservationAdmin(SimpleHistoryAdmin, ReservationImportExport):
    fields = ["delivered","date_reserved",...]

### demo-reservations.yaml  (Note: Problem happens using different data file formats)
 - reservation: 50001
   delivered: False
   date_reserved: 7/15/2022T00:00:00+00:00
   ...

Here's the error (slightly obfuscated)

Line number: 1 - localtime() cannot be applied to a naive datetime
50001, False, 7/15/2022T00:00:00+00:00, CHRIS EDWARDS, 16, ROSE TYLER
Traceback (most recent call last):
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 670, in import_row
diff = self.get_diff_class()(self, original, new)
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 221, in __init__
self.left = self._export_resource_fields(resource, instance)
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 242, in _export_resource_fields
return [resource.export_field(f, instance) if instance else "" for f in resource.get_user_visible_fields()]
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 242, in <listcomp>
return [resource.export_field(f, instance) if instance else "" for f in resource.get_user_visible_fields()]
File "c:\Users\...\lib\site-packages\import_export\resources.py", line 882, in export_field
return field.export(obj)
File "c:\Users\...\lib\site-packages\import_export\fields.py", line 125, in export
return self.widget.render(value, obj)
File "c:\Users\...\lib\site-packages\import_export\widgets.py", line 236, in render
value = timezone.localtime(value)
File "c:\Users\...\lib\site-packages\django\utils\timezone.py", line 206, in localtime
raise ValueError("localtime() cannot be applied to a naive datetime")
ValueError: localtime() cannot be applied to a naive datetime

Tried Already:

  1. Removed SimpleHistoryAdmin from Admin registration
  2. Changed import field to just 7/15/2022
  3. Removing field from import file all together

Thanks All!

JoeF
  • 53
  • 7
  • Please check out [this issue](https://github.com/django-import-export/django-import-export/issues/1165) and [PR](https://github.com/django-import-export/django-import-export/pull/1478) – Matthew Hegarty Jul 30 '22 at 10:19

1 Answers1

1

Sorry I read closer: date_reserved = models.DateTimeField('date reserved', default=datetime.datetime.now)

That causes your error. You don't have to set a default you would just do this: models.DateTimeField(auto_now_add=True) which adds the current datetime (aware) like you are trying to do there.

Here still my old answer for the record:

from django.utils import timezone

tz = timezone.get_current_timezone()
aware_datetime = timezone.make_aware(datetime_object, tz)

This code should solve your Problem. You you have to parse your date time string and insert it into make_aware

You can do something like this if you are still just using the date:

from datetime import datetime

datetime_object = datetime.strptime("7/15/2022", "%m/%d/%Y")

You could also add a function to your model that automatically converts naive datetimes to aware datetime, check this answer if you want to implement this

Karl
  • 146
  • 1
  • 1
  • 12