Django管理面板django.db.utils.IntegrityError:FOREIGN KEY约束失败

时间:2018-03-19 09:26:36

标签: python django python-3.x django-admin django-2.0

我在尝试从管理面板添加模型元素时使用django 2.0版获取完整性错误。 虽然我可以从shell添加元素。

models.py

class Book(models.Model):
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

admin.py

admin.site.register(Book)

我正在使用sqlite数据库

从管理面板添加时出现以下错误

Internal Server Error: /admin/accounts/book/add/
Traceback (most recent call last):
  File "/myproject/env/lib/python3.4/site-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
sqlite3.IntegrityError: FOREIGN KEY constraint failed

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

Traceback (most recent call last):
  File "/myproject/env/lib/python3.4/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/myproject/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/myproject/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/myproject/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 574, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/myproject/env/lib/python3.4/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/myproject/env/lib/python3.4/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/myproject/env/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 223, in inner
    return view(request, *args, **kwargs)
  File "/myproject/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1553, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/myproject/env/lib/python3.4/site-packages/django/utils/decorators.py", line 62, in _wrapper
    return bound_func(*args, **kwargs)
  File "/myproject/env/lib/python3.4/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/myproject/env/lib/python3.4/site-packages/django/utils/decorators.py", line 58, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/myproject/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1450, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/myproject/env/lib/python3.4/site-packages/django/db/transaction.py", line 212, in __exit__
    connection.commit()
  File "/myproject/env/lib/python3.4/site-packages/django/db/backends/base/base.py", line 261, in commit
    self._commit()
  File "/myproject/env/lib/python3.4/site-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
  File "/myproject/env/lib/python3.4/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/myproject/env/lib/python3.4/site-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
django.db.utils.IntegrityError: FOREIGN KEY constraint failed

0 个答案:

没有答案