2つのモデルがあります。外部キーを介して接続されているUser(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)
ユーザーモデルには、username、first_name、last_name、passwordなどのフィールドが含まれています。
私の見解では、ユーザーオブジェクトにあるユーザー名フィールドを使用してUserProfileオブジェクトを検索したいと思います。ユーザー名フィールドを使用して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)
誰かがこれを解決する方法を知っていますか?ありがとう!