3

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

/admin/main/customuser/add/ 处的 IntegrityError

������������:插入 ������ 更新 �� �������������� "django_admin_log" ���������� ������ ���������������������� ���������������� ������ ����“django_admin_log_user_id_fkey”详细信息:�������� (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()

我正在使用带有psycopg2的Postgres ,但无法理解问题出在哪里。在django方面,在Postgres或可能在psycopg2中?

另外,当我再次登录管理员时,响应用户已成功添加,但数据库表中没有新用户。

4

1 回答 1

1

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

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
于 2013-07-22T19:31:24.960 回答