唯一约束失败:auth_user.username [英] UNIQUE constraint failed: auth_user.username

查看:96
本文介绍了唯一约束失败:auth_user.username的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试直接将来自Facebook API的名字和姓氏存储到用户身份验证模型(使用FacebookProfile模型扩展,其中包含webpull,id和year_formed)

I'm trying to store the First name and last name straight from the Facebook API to a User Auth model (which is extended with FacebookProfile model, containing webpull, id and year_formed)

Models.py

class FacebookProfile(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)


    id = models.PositiveIntegerField(primary_key = True)
    #name = models.CharField(max_length = 200, null = True)
    year_formed = models.PositiveIntegerField(default = 0)
    webpull= models.CharField(max_length =1000, null = True)

Views.py

if request.method == 'GET':
    print 'im here'

    return render(request, "logV2.html")
if request.method == "POST":
    first_name = request.POST.get('first_name')
    last_name = request.POST.get('last_name')
    print first_name, last_name

    facebook_user = FacebookUserForm(data=request.POST)
    facebook_profile = FacebookProfileForm()

    has_account = authenticate(first_name = first_name, last_name = last_name)

    if has_account:
        print 'this has account'
        login(request, has_account)
        return HttpResponseRedirect('/music/home/')
    else:
        id_ = request.POST.get('id')
        birthday = request.POST.get('year_formed')
        webpull = request.POST.get('webpull')

        if birthday == "undefined":
            print 'im emplty'   
            year_formed = random.randint(1993,1998)
        else:
            year_formed = re.findall(r"[0-9][0-9][0-9][0-9]$", birthday)[0]

        print id_, year_formed, webpull

        print facebook_user

        user = facebook_user.save()
        profile = facebook_profile.save(commit = False)
        profile.user = user
        profile.webpull = webpull
        profile.id = id_

        ## steal birtday fucntion from log 
        # move to new database facebook (neeed to change all artists to facebookprofile)
        profile.year_formed = year_formed
        profile.save()



        #authenticate user. then log him in.
        #user = authenticate(username = profile.user.username)
        now_has_account = authenticate(first_name = first_name, last_name = last_name)
        login(request, now_has_account)


        #profile.save()
        return HttpResponseRedirect('/music/home/')

在视图中,代码在 user = facebook_user.save()

我试图清除整个数据库,

I tried clearing the whole database,

我从html接收的是带有first_name,last_name,id,year_formed,webpull的表单.数据到达后端就好了.

What I'm receiving from the html is a form with first_name,last_name,id,year_formed,webpull. The data gets to the backend fine.

Forms.py

class FacebookUserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name')

class FacebookProfileForm(forms.ModelForm):
    class Meta:
        model = FacebookProfile
        fields = ('id', 'year_formed', 'webpull',)

即时身份验证是什么 Auth_Backend.py

class OnlynameandsurnameAuth(ModelBackend):
    def authenticate(self, first_name = None, last_name = None):
        try:
            return User.objects.get(first_name = first_name, last_name = last_name)
        except:
            return None

然后 auth.py

admin.site.register(FacebookProfile)

后端认证 settings.py

AUTHENTICATION_BACKENDS = (
    # ... your other backends
    #'music.auth_backend.PasswordlessAuthBackend',
    'music.auth_backend.OnlynameandsurnameAuth',
)

有什么想法可以在没有UNIQUE错误的情况下保存名字和姓氏吗?

any ideas how to save the first_name and last_name without having a UNIQUE error?

谢谢!:D

推荐答案

如果您使用默认的身份验证功能,则不提供密码应该总是(?)失败,这意味着

If you use the default authenticate function not providing a password should always (?) fail, which means that

has_account = authenticate(first_name = first_name, last_name = last_name)

始终为无.

但是主要的问题是您没有为新用户设置用户名,只有first_name和last_name.这将工作一次,但是在创建了一个用户名为空的用户后,下一次尝试将失败,因为用户需要唯一的用户名.所以:添加一个用户名!

But the main problem is that you do not set a username for the new User, only first_name and last_name. This will work once, but after one User with an empty username was created the next attempt will fail, as Users need an unique username. So: Add a username!

除此之外,我认为

user = facebook_user.save()

不将用户分配给用户",而是分配给表单.您应该使用facebook_user.instance.

does not assign the User to "user" but the Form. You should use facebook_user.instance.

这篇关于唯一约束失败:auth_user.username的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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