如何通过 Django 中的 Mixins 更新用户配置文件
How to update user profile through Mixins in Django
需要使用 mixins
编辑 user profile
,方法是向相应的 url 发送 PUT
请求(其中包含其用户的 ID信息待更新)
下面的代码中missing/needs要改变什么来实现上面的功能?
models.py:
class UserProfile(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User)
profile_picture = models.ImageField(upload_to='documents', blank=True)
def __str__(self):
return self.user.username
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username','password', 'first_name', 'last_name', 'email',)
write_only_fields = ('password',)
read_only_fields = ('is_staff', 'is_superuser', 'is_active', 'date_joined',)
def restore_object(self, attrs, instance=None):
user = super(UserSerializer, self).restore_object(attrs, instance)
user.set_password(attrs['password'])
return user
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = UserProfile
fields = ('id','user','profile_picture',)
views.py
class UserMixin(object,):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
permission_classes = (IsOwnerOrReadOnly,)
class UserProfileView(UserMixin, RetrieveUpdateDestroyAPIView):
pass
请询问是否需要更清楚。
您可以使用 ModelViewsets 来更新用户配置文件
class UserProfileViewViewSet(viewsets.ModelViewSet):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
permission_classes = (IsOwnerOrReadOnly,)
def update(self, request, *args, **kwargs):
user_profile = self.get_object()
serializer = self.get_serializer(user_profile, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
return Response(serializer.data)
需要使用 mixins
编辑 user profile
,方法是向相应的 url 发送 PUT
请求(其中包含其用户的 ID信息待更新)
下面的代码中missing/needs要改变什么来实现上面的功能?
models.py:
class UserProfile(models.Model):
id = models.AutoField(primary_key=True)
user = models.OneToOneField(User)
profile_picture = models.ImageField(upload_to='documents', blank=True)
def __str__(self):
return self.user.username
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username','password', 'first_name', 'last_name', 'email',)
write_only_fields = ('password',)
read_only_fields = ('is_staff', 'is_superuser', 'is_active', 'date_joined',)
def restore_object(self, attrs, instance=None):
user = super(UserSerializer, self).restore_object(attrs, instance)
user.set_password(attrs['password'])
return user
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = UserProfile
fields = ('id','user','profile_picture',)
views.py
class UserMixin(object,):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
permission_classes = (IsOwnerOrReadOnly,)
class UserProfileView(UserMixin, RetrieveUpdateDestroyAPIView):
pass
请询问是否需要更清楚。
您可以使用 ModelViewsets 来更新用户配置文件
class UserProfileViewViewSet(viewsets.ModelViewSet):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer
permission_classes = (IsOwnerOrReadOnly,)
def update(self, request, *args, **kwargs):
user_profile = self.get_object()
serializer = self.get_serializer(user_profile, data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
return Response(serializer.data)