使用Django 2.0.5,我在管理面板中遇到了问题。通过管理面板添加或保存数据会出现“ django.db.utils.IntegrityError:FOREIGN KEY约束失败”错误。在模型中,没有可引用的外键。
models.py
class JobTitle(models.Model):
job_title = models.CharField(max_length=200)
admin.py
class JobTitleAdmin(admin.ModelAdmin):
list_display = ['job_title',]
admin.site.register(JobTitle, JobTitleAdmin)
使用前端的PUSH添加相同的数据是可行的,只有在管理面板中才会出现此错误。
编辑:
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/login_app/locations/add/
Django Version: 2.0.5
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'login_app',
'corsheaders',
'rest_auth']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py" in _commit
239. return self.connection.commit()
The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception:
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
574. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view
1553. return self.changeform_view(request, None, form_url, extra_context)
File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
62. return bound_func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/utils/decorators.py" in bound_func
58. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view
1450. return self._changeform_view(request, object_id, form_url, extra_context)
File "/usr/local/lib/python3.6/site-packages/django/db/transaction.py" in __exit__
212. connection.commit()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py" in commit
261. self._commit()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py" in _commit
239. return self.connection.commit()
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py" in _commit
239. return self.connection.commit()
Exception Type: IntegrityError at /admin/login_app/locations/add/
Exception Value: FOREIGN KEY constraint failed
答案 0 :(得分:0)
AFAIK管理界面还提供了某种历史跟踪功能(例如,保存对象的时间)。
由于FK错误似乎没有从代码中弹出(基于跟踪),因此我认为您需要(重新)运行manage.py migrate
(甚至是makemigrations
)来更新您的代码数据库布局。