TyCharm
TyCharm

Reputation: 425

Django: Add additional properties to User

I'm trying to add additional attributes to my "Person" model, namely, "age", "city", and "state." I've been struggling with this for a few days now and have looked up the documentation on how to "Extend the User class" in Django. But, I'm stuck, and when I try to create a new account I get the following error:

TypeError at /polls/signup/add
'age' is an invalid keyword argument for this function

Person model:

class Person(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    city = models.CharField(max_length=200, null=True)
    state = models.CharField(max_length=200, null=True)
    age = models.CharField(max_length=50, null=True)

Create account view (I'm pretty sure this is where the problem is occurring):

def create_account(request):
    if request.method == 'POST':
        new_user = User(username = request.POST["username"],
                    email=request.POST["email"],
                    first_name=request.POST["first_name"],
                    last_name=request.POST["last_name"],
                    age=request.POST["age"],
                    city=request.POST["city"],
                    state=request.POST["state"])
        new_user.set_password(request.POST["password"])
        new_user.save()
        Person.objects.create(user=new_user,
                          first_name=str(request.POST.get("first_name")),
                          last_name=str(request.POST.get("last_name")),
                          email=str(request.POST.get("email")),
                          age=str(request.POST.get("age")),
                          city=str(request.POST.get("city")),
                          state=str(request.POST.get("state")))
        new_user.is_active = True
        new_user.save()
        return redirect('../')
    else:
        return render(request, 'polls/create_account.html')

Any ideas on how I can solve this problem and allow users to add these bonus fields that aren't included with the generic User model?

Upvotes: 0

Views: 384

Answers (1)

Alexander Sysoev
Alexander Sysoev

Reputation: 835

You should use the Person model instead of User.

if request.method == 'POST':
    new_user = User(username = request.POST["username"],
                    email=request.POST["email"],
                    first_name=request.POST["first_name"],
                    last_name=request.POST["last_name"],
                    )
    new_user.set_password(request.POST["password"])
    new_user.save()
    Person.objects.create(user=new_user,
                      age=str(request.POST.get("age")),
                      city=str(request.POST.get("city")),
                      state=str(request.POST.get("state")))
    new_user.is_active = True
    new_user.save()
    return redirect('../')
else:
    return render(request, 'polls/create_account.html')

Upvotes: 2

Related Questions