DRF serializer.save()对象不可迭代

时间:2015-07-07 16:32:27

标签: django django-rest-framework

当我尝试将扩展数据保存到ModelsetView中的对象

def perform_create(self, serializer):
        serializer.save(from_user=self.request.user)

我收到错误: '用户'对象不可迭代

DRF 3.1.3版 我做错了什么?

class MailViewSet(viewsets.ModelViewSet):
    model = Mail
    queryset = Mail.objects.all()
    serializer_class = CreateMailSerializer

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return CreateMailSerializer
        return MailSerializer


    def perform_create(self, serializer):
        serializer.save(from_user=self.request.user)


class Mail(models.Model):
    name = models.CharField(max_length=200)
    text = models.TextField()
    from_user = models.ForeignKey(User, related_name='from_users')
    to_users = models.ManyToManyField(User, blank=True, null=True, related_name='to_users')



class CreateMailSerializer(serializers.ModelSerializer):
    to_users = EmailRelationField(queryset=User.objects.all())

    class Meta:
        model = Mail

0 个答案:

没有答案