UNIQUE约束失败:auth_user.username

时间:2016-12-24 13:18:38

标签: python django facebook-graph-api

我正在尝试将名字和姓氏直接从Facebook API存储到User Auth模型(使用FacebookProfile模型扩展,包含webpull,id和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()

处制动

我尝试清除整个数据库,

我从html收到的是带有first_name,last_name,id,year_formed,webpull的表单。数据到达后端很好。

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错误的情况下保存first_name和last_name的任何想法?

谢谢! :d

1 个答案:

答案 0 :(得分:2)

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

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

永远都是无。

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

除此之外,我认为

user = facebook_user.save()

不会将用户分配给"用户"但形式。 你应该使用facebook_user.instance。