Django ORM在尝试使用具有附加信息的用户时出错
我在Ubuntu12.04上使用Django1.4和Python2.7 编辑:我正在清除这个问题中的一些错误部分,因为似乎相同的问题再次出现。它工作得很好-我没有做任何改变-现在它失败了。怎么回事 这基本上就是视图的外观:Django ORM在尝试使用具有附加信息的用户时出错,django,django-models,Django,Django Models,我在Ubuntu12.04上使用Django1.4和Python2.7 编辑:我正在清除这个问题中的一些错误部分,因为似乎相同的问题再次出现。它工作得很好-我没有做任何改变-现在它失败了。怎么回事 这基本上就是视图的外观: @login_required def request_new_project(request): """ .. function:: request_new_project() Collect information to call th
@login_required
def request_new_project(request):
"""
.. function:: request_new_project()
Collect information to call the form used to create a new project
:param request: Django Request object
"""
user_dict = { 'rsb_username' : request.user.username}
form = CreateProject(initial = user_dict)
data = { 'user' : request.user }
data.update(csrf(request))
data.update({ 'form' : form })
return render_to_response("create_project.html", data)
@login_required
def add_project(request):
"""
.. function:: add_project()
Add a project for the user
:param request: Django Request object
"""
if (request.method == "POST"):
user = User.objects.get(username = request.POST.get('rsb_username'))
userProfile = UserProfile.objects.get(user = user)
new_project = Projects(client = userProfile,
project_name = request.POST.get('rsb_project_name'),
description = request.POST.get('rsb_description'),
budget = request.POST.get('rsb_budget'),
time_frame = request.POST.get('rsb_time_frame'),
time_frame_units = request.POST.get('rsb_time_frame_units'),
contact = request.POST.get('rsb_point_of_contact'),
contact_email = request.POST.get('rsb_contact_email'),
contact_phone = request.POST.get('rsb_contact_phone'),
price_quote = 0,
eta = 'To Be Determined',
current_status = 'Waiting for quote',
)
new_project.save()
return view_projects(request)
我得到以下错误:
Cannot assign "<UserProfile: UserProfile object>": "Projects.client" must be a "User" instance.
有什么建议吗
更新1:
从实际数据库中,我可以看到rsb_项目的约束之一是:
rsb\u项目\u客户端\u id\u fkey(客户端\u id)参考rsb\u用户配置文件(id)匹配简单更新无操作删除无操作延迟初始延迟
如果这有帮助的话
在我看来,即使我在models.py
中定义了ForeignKey
来对抗User
,数据库还是需要UserProfile
id
想法?错误正是它所说的
为了创建新项目,您需要为项目对象指定一个用户配置文件对象,而不是用户配置文件id
user = User.objects.get(id=7)
userprofile = user.get_profile()
project.client = userprofile
请注意,分配给项目实例的客户机属性的是userprofile
对象。无法将userprofile
对象的id分配给项目实例的客户端属性
另外,不要将您的用户id与您的用户配置文件id混淆。它们可能不完全相同
用户id是每当在数据库中创建新用户时在auth_user表中自动生成的主键。
每当创建与用户相关的相应用户配置文件时,userprofile id是profiles\u userprofile表中自动生成的主键
换句话说,您的add\u项目
view函数需要如下内容
if (request.method == "POST"):
user = User.objects.get(username = request.POST.get('rsb_username'))
# userprofile = UserProfile.objects.get(user = user) <-- we don't need this anymore.
new_project = Projects(client = user, # <--- give it a user instance of course now that your model has changed
project_name = request.POST.get('rsb_project_name'),
description = request.POST.get('rsb_description'),
budget = request.POST.get('rsb_budget'),
time_frame = request.POST.get('rsb_time_frame'),
time_frame_units = request.POST.get('rsb_time_frame_units'),
contact = request.POST.get('rsb_point_of_contact'),
contact_email = request.POST.get('rsb_contact_email'),
contact_phone = request.POST.get('rsb_contact_phone'),
)
new_project.save()
if(request.method==“POST”):
user=user.objects.get(username=request.POST.get('rsb_username'))
#userprofile=userprofile.objects.get(user=user)在您的settings.py文件中,指定AUTH\u PROFILE\u MODULE='profiles.userprofile'
注意,“profiles”将是您命名的userprofile应用程序。如果您的用户配置文件应用程序名为userprofile,那么它应该是AUTH\u profile\u MODULE='userprofile.userprofile'
django文档中描述了指定此设置的目的。请忽略我以前(现在已删除)的注释。它是在你回答完你的问题之前发布的,绝对是。再说一次,我还没有完全理解它,但我要花点时间来理解它!谢谢。在django项目中使用“/manageshell”,了解“对象”和“id”(整数)之间的区别。它们是两种不同的东西。当指定ForeignKey字段时,我们本质上是在说我们链接到一个对象,而不是对象的id。这里似乎有其他问题?我不知道为什么它突然停止工作了=-/
if (request.method == "POST"):
user = User.objects.get(username = request.POST.get('rsb_username'))
# userprofile = UserProfile.objects.get(user = user) <-- we don't need this anymore.
new_project = Projects(client = user, # <--- give it a user instance of course now that your model has changed
project_name = request.POST.get('rsb_project_name'),
description = request.POST.get('rsb_description'),
budget = request.POST.get('rsb_budget'),
time_frame = request.POST.get('rsb_time_frame'),
time_frame_units = request.POST.get('rsb_time_frame_units'),
contact = request.POST.get('rsb_point_of_contact'),
contact_email = request.POST.get('rsb_contact_email'),
contact_phone = request.POST.get('rsb_contact_phone'),
)
new_project.save()