Problem importing data using django-import-export error NOT NULL constraint failed:

laawzig2  于 2023-04-22  发布在  Go
关注(0)|答案(1)|浏览(176)

我已经在我的项目中设置了django-import-export应用程序,并一直试图从Excel电子表格导入数据,但没有成功。我删除了我的迁移文件和数据库,并再次生成它们,看看是否有帮助,但仍然是同样的错误。
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

我的电子表格中的第1行和第2行:

尝试导入时管理站点中的错误消息:

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

我尝试从1开始将主键ID列添加到我的电子表格中,但没有任何区别。在我所有的研究中,我尝试的每一个解决方案都没有成功。如果有人能看到我哪里出错了,你的建议将是伟大的。

2ledvvac

2ledvvac1#

我犯了一个错误。它也显示这个错误只是因为我的csv文件有问题。“weibo_content,weibo_forward,weibo_comments,weibo_like”。后面不应该有空格,

相关问题