0

関連するプロファイルを持つユーザーを取得する方法は次のとおりです: Django: User.objects.profile.all() と User.objects.get_profile() の違い?

ただし、この例ではユーザー ID を使用しています。activation_keyUserProfile オブジェクトにあるユーザーを取得したいので、次のようにします。

UserProfile.objects.select_related('user').get(activation_key=key)

しかし、私は得ています:

UserProfile 一致クエリが存在しません。

4

2 に答える 2

2

次の例のように、 UserProfile に関連付けられた django.contrib.auth.models.User オブジェクトを取得しようとしている場合:

from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    activation_key = models.CharField(...)

私がすることは次のとおりです。

user_profile = UserProfile.objects.get(activation_key='some_key')
user = user_profile.user

それがあなたが探していたものであることを願っています

于 2012-10-23T18:38:02.647 に答える
0
user = User.objects.get(userprofile__activation_key='key')
于 2012-10-23T22:47:46.290 に答える