简体   繁体   中英

how do i get the string inside the charfield in django?

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)

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)

Assuming person is a Person instance:

name = person.name
email = person.email 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM