UNIQUE constraint failed: auth_user.username

≡放荡痞女 提交于 2021-01-27 12:54:43

问题


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/')

In the views the code brakes at user = facebook_user.save()

I tried clearing the whole database,

also

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',)

what im authenticating 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

then auth.py

admin.site.register(FacebookProfile)

the backend authentification settings.py

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

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

Thanks! :D


回答1:


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)

always will be None.

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!

Besides that, I think that

user = facebook_user.save()

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



来源:https://stackoverflow.com/questions/41313621/unique-constraint-failed-auth-user-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!