gpt4 book ai didi

python - Django 管理编码错误

转载 作者:行者123 更新时间:2023-11-28 18:47:29 25 4
gpt4 key购买 nike

我正在尝试使用管理界面添加新用户并收到此错误:

IntegrityError at /admin/main/customuser/add/

������������: INSERT ������ UPDATE �� �������������� "django_admin_log" ���������������� ���������������������� ���������������� ���������� "django_admin_log_user_id_fkey" DETAIL: �������� (user_id)=(1) ���������������������� �� �������������� "auth_user".

这是回溯:

     File "C:\Python33\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python33\lib\site-packages\django\contrib\admin\options.py", line 372, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python33\lib\site-packages\django\utils\decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Python33\lib\site-packages\django\views\decorators\cache.py", line 89, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Python33\lib\site-packages\django\contrib\admin\sites.py", line 202, in inner
return view(request, *args, **kwargs)
File "C:\Python33\lib\site-packages\django\utils\decorators.py", line 25, in _wrapper
return bound_func(*args, **kwargs)
File "C:\Python33\lib\site-packages\django\utils\decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Python33\lib\site-packages\django\utils\decorators.py", line 21, in bound_func
return func(self, *args2, **kwargs2)
File "C:\Python33\lib\site-packages\django\db\transaction.py", line 223, in inner
return func(*args, **kwargs)
File "C:\Python33\lib\site-packages\django\db\transaction.py", line 217, in __exit__
self.exiting(exc_value, self.using)
File "C:\Python33\lib\site-packages\django\db\transaction.py", line 281, in exiting
commit(using=using)
File "C:\Python33\lib\site-packages\django\db\transaction.py", line 152, in commit
connection.commit()
File "C:\Python33\lib\site-packages\django\db\backends\__init__.py", line 241, in commit
self._commit()
File "C:\Python33\lib\site-packages\django\db\backends\postgresql_psycopg2\base.py", line 242, in _commit
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "C:\Python33\lib\site-packages\django\utils\six.py", line 328, in reraise
raise value.with_traceback(tb)
File "C:\Python33\lib\site-packages\django\db\backends\postgresql_psycopg2\base.py", line 240, in _commit
return self.connection.commit()

我正在使用 Postgrespsycopg2 并且无法理解问题出在哪里。在 django 方面,在 Postgres 或可能在 psycopg2 中?

同样的,当我再次登陆admin时,提示添加用户成功,但数据库表中没有新用户。

最佳答案

我还没有解决数据库响应中的编码问题,但我已经解决了一般问题。我添加了自定义身份验证后端,不再有来自数据库的错误消息。这是我解决问题的示例后端:

class CustomBackend (ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
if kwargs:
try:
user = CustomUser.objects.get(id = kwargs['id'])
except CustomUser.DoesNotExist:
#print ("WE ARE HERE!", kwargs)
user = CustomUser(nickName=kwargs['nickName'], id=kwargs['id'])
user.reputation = 0
user.save()
return user
if username == 'admin' and password == 'admin':
return CustomUser.objects.get(nickName='m9')

def get_user(self, user_id):
try:
return CustomUser.objects.get(id=user_id)
except CustomUser.DoesNotExist:
return None

关于python - Django 管理编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17773006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com