user216171
user216171

Reputation: 1726

Custom create_user() value?

Registration usually takes 3 arguments.

-Username

-Password

-Email

But when i try to add a 4th value, it returns this error:

create_user() got an unexpected keyword argument 'hobby'

Any idea how to solve this?

Here's the form i'm using:

    def save(self):
        new_user = User.objects.create_user(
          username=self.cleaned_data['username'],
          password=self.cleaned_data['password1'],
          email=self.cleaned_data['email'],
          hobby=self.cleaned_data['hobby'])
        return new_user

And here's the view i'm using

def register_page(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/")
    else:
        form = RegistrationForm()
    return render_to_response("registration/register.html", {'form': form})

To Answer my own question and perhaps to help others with a similar problem, here's what i did:

    def save(self):
        new_user = User.objects.create_user(
          username=self.cleaned_data['username'],
          password=self.cleaned_data['password1'],
          email=self.cleaned_data['email'])
        new_user.hobby=self.cleaned_data['hobby']
        new_user.save()
        return new_user

Upvotes: 2

Views: 601

Answers (1)

Seb
Seb

Reputation: 17815

You cannot change User model, but you can attach a profile to it that will hold all that additional information. Read this for more.

Upvotes: 2

Related Questions