0

I'm trying to create a user in my model by calling the create_user function but I want it to have properties of what the user enters into my model. How do I do this?

This is what I have:

class Person(models.Model):       
    #basic information
    name = models.CharField(max_length=50)
    email = models.CharField(max_length=50, unique=True)
    phone_number = PhoneNumberField(unique=True)

    # FIX THIS!
    user = User.objects.create_user(name, email, phone_number)
Matt M
  • 149
  • 2
  • 4
  • 17

2 Answers2

2

Looking at the definition. The signature of create_user is:

def create_user(self, username, email=None, password=None, **extra_fields)

username is a required field

This has to be done in your view -

user = User.objects.create_user(first_name=name, last_name='', email=email, username=username)
karthikr
  • 97,368
  • 26
  • 197
  • 188
1

Assuming person is a Person instance:

name = person.name
email = person.email 
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • How do I initialize that instance? I get a Person is not defined Name error. – Matt M May 07 '13 at 19:50
  • @MattM: Generally speaking, `person = Person.objects.create(name='name', email='email', phone_number=phone)`, but it's hard to say for your specific case without more information. – mipadi May 07 '13 at 19:51
  • What information do you need to know? I'm using the default User model. My admin just registers a model and the admin associated with it. – Matt M May 07 '13 at 19:53
  • @MattM: Can you provide the code sample where you're trying to do this? – mipadi May 07 '13 at 19:54
  • I'm trying to do it in the model class, shown in the original post. – Matt M May 07 '13 at 19:58
  • Basically, my goal is to create a user with every person that is created, such that they can log into the admin site and eventually add limiting permissions. – Matt M May 07 '13 at 19:58