Python 创建新用户时如何创建用户配置文件

Python 创建新用户时如何创建用户配置文件,python,django,django-models,Python,Django,Django Models,我使用django 1.7.4作为我的angular应用程序的服务器。我需要为用户存储更多数据,因此我添加了一个UserProfile模型 models.py from django.db import models from django.contrib.auth.models import User class UserProfile(models.Model): user = models.OneToOneField(User) company_name = models

我使用django 1.7.4作为我的angular应用程序的服务器。我需要为用户存储更多数据,因此我添加了一个
UserProfile
模型

models.py

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

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    company_name = models.CharField(max_length=100)
我想测试一下,看看它是如何从shell中工作的

如何在创建用户时创建用户配置文件

我试过:

>>> u = User.objects.create(username="testing123", email="testing123@gmail.com")
>>> u.userprofile.create(company_name="onetwo")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/bhaarat/.virtualenvs/djangorest/lib/python2.7/site-packages/django/db/models/fields/related.py", line 428, in __get__
    self.related.get_accessor_name()
RelatedObjectDoesNotExist: User has no userprofile.
u=User.objects.create(username=“testing123”,email=”testing123@gmail.com") >>>u.userprofile.create(公司名称=“onetwo”) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“/Users/bharat/.virtualenvs/djangorest/lib/python2.7/site packages/django/db/models/fields/related.py”,第428行,在__ self.related.get\u访问器\u name() RelatedObjectsDoesNotExist:用户没有用户配置文件。
您正在尝试访问相关对象,但在执行
u.userprofile

请以创建
用户
对象的相同方式进行尝试:

UserProfile.objects.create(user=u, company_name="onetwo")

我得到错误
NameError:name'UserProfile'没有定义
我想这是因为
UserProfile
没有导入。我怎样才能导入它?我试图从演示导入UserProfile中导入
,但仍然出现错误
无法导入name UserProfile
就像导入
User
一样,您需要导入UserProfile,否则python将不知道您引用的是什么模型。是的……我似乎无法找到如何导入它。我尝试了从import UserProfile导入
,但没有成功。请阅读django的基础知识-您应该从中执行
。models import UserProfile