profile = UserProfile.objects.get(....)
私がやろうとしているのは、現在ログインしているユーザーのプロファイルを取得することです。括弧内には何を入れればよいですか?
profile = UserProfile.objects.get(....)
私がやろうとしているのは、現在ログインしているユーザーのプロファイルを取得することです。括弧内には何を入れればよいですか?
ここで説明されているパターンに従っていると仮定します。
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
以下を使用できるはずです。
def my_view(request):
user = request.user
if not isinstance(user, AnonymousUser):
profile = user.get_profile()
# do something with the profile here
else:
# handle anonymous users
一般人、それほど厳しくする必要はありません... 一部の人々は、灰色のアウトラインのチェックマークが押されるべきものであることを実際には知りません.
UserProfile.objects.get(user=request.user)
しかし、それがフィールドの場合、 http://docs.djangoproject.com/en/dev/topics/db/queries/#one-to-one-relationshipsOneToOne
を実行できるはずですrequest.user.userprofile