0

I have set up the django-import-export application in my project and have been trying to import data from an Excel spreadsheet but no luck in getting it to work. I deleted my migration files and data base and generated them again to see if that helped but still the same error.

models.py:

from django.db import models

# Create your models here.
class logTimes(models.Model):
    fast_finished = models.BooleanField(default=False)
    start_date_time = models.DateTimeField('start fast')
    end_date_time = models.DateTimeField('end fast')

0001_initial.py:

# Generated by Django 4.0.4 on 2022-07-13 22:14

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='logTimes',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('fast_finished', models.BooleanField(default=False)),
                ('start_date_time', models.DateTimeField(verbose_name='start fast')),
                ('end_date_time', models.DateTimeField(verbose_name='end fast')),
            ],
        ),
    ]

admin.py:

from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from .models import logTimes

@admin.register(logTimes)
class logTimesAdmin(ImportExportModelAdmin):
    pass

Line 1 and 2 from my spreadsheet:

enter image description here

The error message in the admin site when attempting import:

Line number: 1 - NOT NULL constraint failed: startandstoptimes_logtimes.start_date_time
True, 05/08/2018 17:15:00, 06/08/2018 13:30:00, None
Traceback (most recent call last):
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: startandstoptimes_logtimes.start_date_time

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\rlead\anaconda3\lib\site-packages\import_export\resources.py", line 695, in import_row
self.save_instance(instance, using_transactions, dry_run)
File "C:\Users\rlead\anaconda3\lib\site-packages\import_export\resources.py", line 469, in save_instance
instance.save()
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\models\base.py", line 806, in save
self.save_base(
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\models\base.py", line 857, in save_base
updated = self._save_table(
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\models\base.py", line 1000, in _save_table
results = self._do_insert(
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\models\base.py", line 1041, in _do_insert
return manager._insert(
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\models\query.py", line 1434, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1621, in execute_sql
cursor.execute(sql, params)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\utils.py", line 103, in execute
return super().execute(sql, params)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\rlead\anaconda3\lib\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: startandstoptimes_logtimes.start_date_time

I tried adding the Primary key ID column to my spreadsheet starting from 1 but that made no difference. In all my research, every solution I have tried has not been successful. If someone can see where I am going wrong, your advice would be great.

2
  • 1
    Hard to say for sure but it looks like there's an extra field in your spreadsheet which is None. Have you declared a Resource? This will enable you to control which fields are imported and how they are represented. I suggest add the Resource definition to your question. Commented Jul 15, 2022 at 9:50
  • Thank you, I didn't create a Resources file I was researching this further last night and started to think this may be what I needed to control the data from the Excel Sheet. What I did was convert my XLSX sheet to a CSV and I saw all the extra commas in the last column and hundreds of rows so there was definitely stuff there that I wasn't expecting. I'll try and figure out how to implement a Resource file. Thanks! Commented Jul 15, 2022 at 16:49

2 Answers 2

0

I made a blunder. It also show this fault simply because my csv file has something wrong."weibo_content, weibo_forward, weibo_comments, weibo_like".there should not have a blank space after ,

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

The reason for this error is due to the field you created with datetimeField. Change side null=True or assign a value.

models.DateTimeField(null=True)

or

models.DateTimeField(auto_now_add=True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.