TypeError:无法散列的类型:'collections.OrderedDict'

时间:2019-05-29 20:59:28

标签: django python-3.x django-rest-framework

每当我尝试将用户对象发送到members字段时,我都会遇到此错误。

我尝试通过终端保存数据,但是当我从django rest发布数据时,它给了我错误。

查看图片以供参考:

enter image description here

查看错误以供参考:

enter image description here

代码:

class ProfileSerializer(serializers.ModelSerializer):
    """Profile Serializer"""
    class Meta:
        model = Profile
        fields = ('user','profile_name','image','profile_title')
        extra_kwargs = {
            'image': {'read_only': True},
            'profile_name': {'read_only': True},
            'profile_title': {'read_only': True},
        }


class FriendsSerializer(serializers.ModelSerializer):
    """Serializer For Team Mambers"""
    members = ProfileSerializer(many=True)

    class Meta:
        model = Friend
        fields = '__all__'
    def create(self, validated_data):
        print(validated_data)
        user = validated_data['user']
        members = validated_data['members']

        instance = Friend.objects.create(user=user)
        instance.members.add(*members)
        return instance



class FriendsViewSet(ModelViewSet):
    """ViewSet For TeamMembers Model"""

    serializer_class = FriendsSerializer
    lookup_field = 'user'
    queryset = Friend.objects.all()


class Friend(models.Model):
    user = models.OneToOneField(User, related_name='creator', on_delete=models.CASCADE)
    members = models.ManyToManyField(Profile, blank=True)

    def __str__(self):
        return str(self.user.username)

0 个答案:

没有答案