モデルに追加しようとしuserprofile
ていますuser
使用:
django レスト フレームワーク。
rest-auth モジュール
しかし、エラーprofile = instance.userprofile
を与える行:*** django.db.models.fields.related.RelatedObjectDoesNotExist: User has no userprofile.
ここからの指示に従います
また、super
声明で何が起こっているのかわからない
考えられるエラー:
1.ステートメントの後にinstance
ないため、エラーを与えるステートメント2.を追加する必要がありますuserprofile
super
profile = instance.userprofile
userprofile
UserDetailsSerializer
UserDetailsSerializer
class UserDetailsSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ('username', 'email', 'first_name', 'last_name')
read_only_fields = ('email', )
ユーザーシリアライザー
class UserSerializer(UserDetailsSerializer):
company_name = serializers.CharField(source="userprofile.company_name")
class Meta(UserDetailsSerializer.Meta):
fields = UserDetailsSerializer.Meta.fields + ('company_name',)
def update(self, instance, validated_data):
profile_data = validated_data.pop('userprofile', {})
company_name = profile_data.get('company_name')
instance = super(UserSerializer, self).update(instance, validated_data)
# get and update user profile
profile = instance.userprofile
if profile_data and company_name:
profile.company_name = company_name
profile.save()
return instance
必要に応じて、より明確にするよう求めてください。
前もって感謝します。