我有两个模型:用户(由 Django 预定义)和通过外键连接的 UserProfile。
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="connect")
location = models.CharField(max_length=20, choices=LOCATION_CHOICES)
gender = models.CharField(max_length=20, choices=GENDER_CHOICES)
User 模型包含诸如用户名、名字、姓氏和密码等字段。
在我看来,我想使用位于用户对象中的用户名字段来查找用户配置文件对象。要使用用户名字段过滤/获取 UserProfile 对象,我需要“遍历”外键以访问用户模型。
但是,我在尝试执行此操作时遇到了几个错误。有谁知道我该怎么做?
这是我到目前为止所拥有的:
def edit(request):
#the line below is where I am facing troubles.
#The error I'm getting is SyntaxError: keyword can't be an expression
user = UserProfile.objects.filter(user.username=request.user.username)
form1 = UserEditForm()
form2 = UserProfileEditForm()
c = RequestContext(request, {
'action': 'update/',
'button': 'Update',
'form1': form1,
'form2': form2,
})
return render_to_response('registration/user_profile.html', c)
有谁知道如何解决这个问题?谢谢!