首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型为ManyToManyField的字段在发送请求时不保存数据。

类型为ManyToManyField的字段在发送请求时不保存数据。
EN

Stack Overflow用户
提问于 2022-11-14 14:10:43
回答 1查看 28关注 0票数 0

基本上,我做的事情就像‘我们自己的存档’,我没有保存或保存工作,但是ManyToMany字段数据没有保存

views.py

代码语言:javascript
复制
if request.method == 'POST':  
    serializer = FanficSerializerCreate(data=request.data)

    if serializer.is_valid():
      serializer.save()
      return Response({'fanfic': serializer.data}, status = status.HTTP_201_CREATED)

models.py

代码语言:javascript
复制
class Fanfic(models.Model):
...
  user = models.ForeignKey(User, on_delete=models.CASCADE)
  title = models.CharField(max_length=200)
  fandoms = models.ManyToManyField(Fandom)
  pairings = models.ManyToManyField(Pairing)
  characters = models.ManyToManyField(Hero)
  tags = models.ManyToManyField(Tag)
....

serializer.py

代码语言:javascript
复制
class FanficSerializerCreate(ModelSerializer):

  fandoms = FandomSerializer(many=True, read_only=True)
  pairings = PairingSerializer(many=True, read_only=True)
  characters = HeroSerializer(many=True, read_only=True)
  tags = TagSerializer(many=True, read_only=True)

  class Meta:
    model = Fanfic
    fields = ['id', 'user', 'title', 'fandoms', 'pairings', 'characters', 'translate', 'categories', 'end', 'relationships', 'tags', 'description', 'note', 'i_write_for', 'created', 'updated']

我认为问题是在另一节的序列化程序中,例如,添加一个具有相同视图代码的字符,但是模型中没有多个manytomanyfield字段就可以了。

当我在serializers.py中写这个的时候

代码语言:javascript
复制
fandoms = FandomSerializer(many=True, read_only=True)
  pairings = PairingSerializer(many=True, read_only=True)
  characters = HeroSerializer(many=True, read_only=True)
  tags = TagSerializer(many=True, read_only=True)

显示

代码语言:javascript
复制
{
   "fanfic": {
      "id": 4,
      "user": 3,
      "title": "Claimed",
      "fandoms": [],
      "pairings": [],
      "characters": [],
      "translate": "NO",
      "categories": "M",
      "end": "ENDED",
      "relationships": "M/M",
      "tags": [],
      "description": "",
      "i_write_for": "...",
      "created": "2022-11-14T13:46:44.425693Z",
      "updated": "2022-11-14T13:46:44.425693Z"
   }
}

id只是没有添加到字段中。

当我在serializers.py中写这个的时候

代码语言:javascript
复制
fandoms = FandomSerializer(many=True)
  pairings = PairingSerializer(many=True)
  characters = HeroSerializer(many=True)
  tags = TagSerializer(many=True)

邮递员

代码语言:javascript
复制
{
   "message": {
      "fandoms": [
         "This field is required."
      ],
      "pairings": [
         "This field is required."
      ],
      "characters": [
         "This field is required."
      ],
      "tags": [
         "This field is required."
      ]
   }
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-14 14:32:54

可以将嵌套序列化器的字段设置为required=False对该字段不需要的字段:

代码语言:javascript
复制
class FanficSerializerCreate(ModelSerializer):

  fandoms = FandomSerializer(many=True, required=False)
  ...

  class Meta:
    model = Fanfic
    ...

此外,您还可以为Meta类中的不同字段使用它:

代码语言:javascript
复制
class FanficSerializerCreate(ModelSerializer):

  fandoms = FandomSerializer(many=True)
  ...

  class Meta:
    model = Fanfic
    extra_kwargs = {
        'fandoms': {'required': False}
        ...
    }
    ...

参考资料:(https://www.django-rest-framework.org/api-guide/fields/#required)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74432987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档