DRF从嵌套表示创建新对象会引发TypeError

2024-09-30 01:36:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下两种型号:

class User(models.Model):
    user_id = models.CharField(
        max_length=129,
        unique=True,
    )
    user_article = models.ManyToManyField(
        Article,
        through="UserArticle",
    )
    occupation = models.CharField(max_length=100, default='null')

    def __str__(self):
        return self.user_id

class Article(models.Model):
    uuid = models.UUIDField(editable=False, unique=True)
    company = models.ForeignKey(
        Company,
        on_delete=models.PROTECT,
        related_name='article_company_id',
    )
    articleType = models.ForeignKey(
        ArticleType,
        on_delete=models.PROTECT,
        related_name='type',
    )    
    date_inserted = models.DateField()    
    def __str__(self):
        return self.uuid

使用以下直通模型,使用多对多关系对其进行建模:

class UserArticle(models.Model):    
    user = models.ForeignKey(User, to_field='user_id',
                             on_delete=models.PROTECT,)
    article = models.ForeignKey(Article, to_field='uuid',
                                 on_delete=models.PROTECT,)
    posted_as = ArrayField(
        models.CharField(max_length=100, blank=True),)
    post_date = models.DateField()

    class Meta:
        db_table = "core_user_articles"

以下是我的看法:

class BatchUserArticleList(mixins.ListModelMixin,
                        mixins.CreateModelMixin,
                        generics.GenericAPIView):
    queryset = UserArticle.objects.all()
    serializer_class = BatchUserArticleSerializer

    def create(self, request, *args, **kwargs):
        serializer = BatchUserArticleSerializer(data=request.data)
        if not serializer.is_valid():
            return response.Response({'Message': 'POST failed',
                                  'Errors': serializer.errors},
                                 status.HTTP_400_BAD_REQUEST)
        self.perform_create(serializer)  # equal to serializer.save()
        return response.Response(serializer.data, status.HTTP_201_CREATED)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

我面临的问题是,当我想在M2M表中发布以下格式的数据时:

{
    "posted_as": ["news"],
    "post_date": "2020-05-26",
    "user": "jhtpo9jkj4WVQc0000GXk0zkkhv7u",
    "article": [
        "11111111",
        "22222222"
    ]
}

由于我没有传递userarticle实体的精确映射(我只传递UID,而不传递其余的模型字段),因此我的序列化程序字段不能是以下内容(我得到了验证错误):

user = UserSerializer()
article = ArticleSerializer()

因此,我使用的serializer如下所示:

class BatchUserArticleSerializer(serializers.ModelSerializer):
    user = serializers.CharField()
    article = serializers.ListField(
        child=serializers.UUIDField()
    )  
    class Meta:
        model = UserArticle
        fields = '__all__'

    def validate(self, data):    
        post_date = data['post_date']
        if post_date != date.today():
            raise serializers.ValidationError(
                'post_date: post_date is not valid',
            )
        return data

    def create(self, validated_data):
        posted_as = list(map(lambda item: item, validated_data['posted_as']))
        post_date = validated_data['post_date']
        user = validated_data['user']
        list_of_articles = validated_data['article']
        user_object = User.objects.get(user_id=user)
        articles_objects = list(map(lambda res: Article.objects.get(uuid=res), list_of_articles))

        user_articles_to_insert = list(map(
            lambda article: UserArticle(
                posted_as=posted_as,
                post_date=post_date,
                article=article,
                user=user_object),
        articles_objects)

        try:
            created_user_articles = UserArticle.objects.bulk_create(user_articles_to_insert) 
            # returning only the first result since create() expects only one object
            return created_user_articles[0] 
        except Exception as error:
            raise Exception('Something went wrong: {0}'.format(error))

这似乎很好,因为我可以看到我的数据被正确地插入了UserArticle表中:

id | posted_as | post_date | user | article
1  | news      | 2020-05-26 | jhtpo9jkj4WVQc0000GXk0zkkhv7u | 11111111
2  | news      | 2020-05-26 | jhtpo9jkj4WVQc0000GXk0zkkhv7u | 22222222

但是,我在响应中得到以下错误:

  File "core/views.py", line 430, in create
    return response.Response(serializer.data, status.HTTP_201_CREATED)
  File "python3.6/site-packages/rest_framework/serializers.py", line 562, in data
    ret = super().data
  File "python3.6/site-packages/rest_framework/serializers.py", line 260, in data
    self._data = self.to_representation(self.instance)
  File "python3.6/site-packages/rest_framework/serializers.py", line 529, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "python3.6/site-packages/rest_framework/fields.py", line 1692, in to_representation
    return [self.child.to_representation(item) if item is not None else None for item in data]
TypeError: 'Article' object is not iterable

如何序列化此嵌套表示?我做错了什么


Tags: toselfdatadatereturnmodelsasarticle
1条回答
网友
1楼 · 发布于 2024-09-30 01:36:18

您应该重写序列化程序的表示方法,因为在调用create方法调用_representation func以将数据返回到客户端之后,并且由于更改了用户序列化程序和项目序列化程序,此方法无法返回真实数据

def to_representation(self, instance):
    return {'result':{'id':instance.id}}

相关问题 更多 >

    热门问题