I am currently facing a very strange error and don’t know what to do.
Summary
As my first Django project I am currently programming a website for a betting game. I manage a large part of the data in the database with the Django admin dashboard for the sake of simplicity.
Up to now I have always managed the data for tests with my first created superuser. Without any problems. Now I have deployed the website and created a second admin user. However, this user cannot make any changes/creations/deletions to any database via the admin dashboard. There is always an IntegrityError: FOREIGN KEY constraint failed (details below).
What I tried:
- I gave admin rights to the user
- I explicitly gave him all change rights again
- I created the second admin user again with “manage.py createsuperuser”
CustomUser Model
Maybe it is important to know that I created a custom user model, which is also registered in settings.py.
I noticed that after the error when reloading the page, the changes/deletions etc. were not applied, but the success message is displayed: “xxxx changed successfully ”.
As I said, Django is still relatively new to me. So please let me know what other information might be important and I will provide it.
Thanks for your time helping me
Error Details
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/main/match/18/change/
Django Version: 5.0.6
Python Version: 3.11.7
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'crispy_bootstrap5',
'main.apps.MainConfig',
'account.apps.AccountConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\db\backends\base\base.py", line 299, in _commit
return self.connection.commit()
^^^^^^^^^^^^^^^^^^^^^^^^
The above exception (FOREIGN KEY constraint failed) was the direct cause of the following exception:
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\contrib\admin\options.py", line 716, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\views\decorators\cache.py", line 80, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\contrib\admin\sites.py", line 240, in inner
return view(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\contrib\admin\options.py", line 1948, in change_view
return self.changeform_view(request, object_id, form_url, extra_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\utils\decorators.py", line 48, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\utils\decorators.py", line 188, in _view_wrapper
result = _process_exception(request, e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\utils\decorators.py", line 186, in _view_wrapper
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\contrib\admin\options.py", line 1803, in changeform_view
with transaction.atomic(using=router.db_for_write(self.model)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\db\transaction.py", line 263, in __exit__
connection.commit()
^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\db\backends\base\base.py", line 323, in commit
self._commit()
^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\db\backends\base\base.py", line 298, in _commit
with debug_transaction(self, "COMMIT"), self.wrap_database_errors:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Samuel\PycharmProjects\Tippspiel-Website\.venv\Lib\site-packages\django\db\backends\base\base.py", line 299, in _commit
return self.connection.commit()
^^^^^^^^^^^^^^^^^^^^^^^^
Exception Type: IntegrityError at /admin/main/match/18/change/
Exception Value: FOREIGN KEY constraint failed
CustomUser from models.py
class CustomUser(AbstractUser):
meistertipp = models.ForeignKey(Mannschaft, related_name='meistertipp_users', null=True, blank=True, on_delete=models.CASCADE)
koenigtipp = models.ForeignKey(Mannschaft, related_name='koenigtipp_users', null=True, blank=True, on_delete=models.CASCADE)
p_1 = models.IntegerField(default=0)
p_2 = models.IntegerField(default=0)
p_3 = models.IntegerField(default=0)
p_4 = models.IntegerField(default=0)
p_5 = models.IntegerField(default=0)
p_total = models.IntegerField(default=0)
def __str__(self):
return self.username
settings.py
...
AUTH_USER_MODEL = 'main.CustomUser'
...