djangodjango-registrationdjango-profiles

django-registration create_user() unexpected keyword


I am trying to create a custom profile to add additional fields to django-registration. Here is the code I have so far --

in models.py

from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.ForeignKey(User, unique=True)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.CharField(max_length=50)
    password = models.CharField(max_length=50) 

and in settings.py

AUTH_PROFILE_MODULE = 'myproject.Profile'

However, I am getting the following error when I try to use create_user(). Here is what happens when I type it into the interpreter --

>>> from django.contrib.auth.models import User
>>> User.objects.create_user(first_name='tom',last_name='smith',email='ts@gmail.com',password='hello')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: create_user() got an unexpected keyword argument 'first_name'

What am I missing in order for create_user() to recognize these new columns? Thank you.


Solution

  • There are a number of things you're doing wrong here

    Firstly, these aren't fields on the User model, they're on the Profile model. So it shouldn't be surprising that the User manager method doesn't recognise them. You'll need to define a User object in the normal way with create_user, then define the Profile instance using that user.

    Secondly, create_user doesn't take all the field names as keyword arguments - it only takes username, email and password. Again, you'll need to set the other fields yourself afterwards.

    However, I don't understand at all why you've defined these particular fields. They are all available in the built-in User model. In particular, you should definitely not be defining your own password field, unless you also write some code to store the password as a hash rather than as plain text.