django allauth空用户名会导致postgress DB中的重复密钥 [英] django allauth empty username causes duplicate key in postgress DB

查看:96
本文介绍了django allauth空用户名会导致postgress DB中的重复密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.8.16
django-allauth 0.27.0
使用postgres作为数据库。



我的应用程序不使用用户名,仅使用e -mail地址作为用户ID。
所以我使用以下设置:

  ACCOUNT_AUTHENTICATION_METHOD ='email'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED =真实
ACCOUNT_UNIQUE_EMAIL =真实
ACCOUNT_EMAIL_VERIFICATION =强制性
ACCOUNT_USER_MODEL_USERNAME_FIELD =无
ACCOUNT_USER_MODEL_EMAIL_FIELD ='电子邮件'


现在,当新用户注册时,他将使用他的电子邮件地址。
但是在提交注册表格时,出现以下错误:

  / accounts / signup / $ b $的IntegrityError b重复的键值违反了唯一约束 auth_user_username_key 
详细信息:键(用户名)=()已存在。
请求方法:POST
请求URL:http://swd.localhost:8000 / accounts / signup /
Django版本:1.8.16
异常类型:IntegrityError
异常值:
重复键值违反了唯一约束 auth_user_username_key
详细信息:键(用户名)=()已存在。

这完全是错误的:auth_user表的用户名字段中已经存在空用户名,似乎不允许这样做?
但是问题是,使用上述设置,用户名字段始终为空。
那么我们该如何解决呢?



我没有调整用户模型。

解决方案

执行此操作...



ACCOUNT_USER_MODEL_USERNAME_FIELD = None



...,您告诉allauth您的用户模型没有 username 字段。在您的情况下,这显然是错误的,因为您在该列上遇到了约束错误。因此,只需将其设置为用户名 而不是,以便allauth将正确填充该字段。


Django 1.8.16 django-allauth 0.27.0 Using postgres as database.

My application does not use usernames, only e-mail addresses as user id. So I use following settings:

ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USER_MODEL_EMAIL_FIELD = 'email'

Now when a new user registers, he uses his e-mail address. But when submitting the registration form, I get this error:

IntegrityError at /accounts/signup/
duplicate key value violates unique constraint "auth_user_username_key"
DETAIL:  Key (username)=() already exists.
Request Method: POST
Request URL:    http://swd.localhost:8000/accounts/signup/
Django Version: 1.8.16
Exception Type: IntegrityError
Exception Value:    
duplicate key value violates unique constraint "auth_user_username_key"
DETAIL:  Key (username)=() already exists.

This says exactly what is wrong: The empty username already exists in the auth_user table, field "username", and it seems this is not allowed? But problem is that the username field is ALWAYS empty using above settings. So how can we get around this?

I did not adapt the user model.

解决方案

By doing this...

ACCOUNT_USER_MODEL_USERNAME_FIELD = None

... you are telling allauth that your user model has no username field. In your case, that is clearly wrong, as you get a constraint error on that column. So, simply set it to "username" instead of None so that allauth will populate the field correctly.

这篇关于django allauth空用户名会导致postgress DB中的重复密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆