<bdo id='PT8yT'></bdo><ul id='PT8yT'></ul>

      <i id='PT8yT'><tr id='PT8yT'><dt id='PT8yT'><q id='PT8yT'><span id='PT8yT'><b id='PT8yT'><form id='PT8yT'><ins id='PT8yT'></ins><ul id='PT8yT'></ul><sub id='PT8yT'></sub></form><legend id='PT8yT'></legend><bdo id='PT8yT'><pre id='PT8yT'><center id='PT8yT'></center></pre></bdo></b><th id='PT8yT'></th></span></q></dt></tr></i><div id='PT8yT'><tfoot id='PT8yT'></tfoot><dl id='PT8yT'><fieldset id='PT8yT'></fieldset></dl></div>

      1. <small id='PT8yT'></small><noframes id='PT8yT'>

      2. <tfoot id='PT8yT'></tfoot>

        <legend id='PT8yT'><style id='PT8yT'><dir id='PT8yT'><q id='PT8yT'></q></dir></style></legend>
      3. 如何使用 Postman 表单数据在 Django REST Framework 中发布嵌套数组?

        时间:2023-09-28

        <legend id='liJQU'><style id='liJQU'><dir id='liJQU'><q id='liJQU'></q></dir></style></legend>
        • <i id='liJQU'><tr id='liJQU'><dt id='liJQU'><q id='liJQU'><span id='liJQU'><b id='liJQU'><form id='liJQU'><ins id='liJQU'></ins><ul id='liJQU'></ul><sub id='liJQU'></sub></form><legend id='liJQU'></legend><bdo id='liJQU'><pre id='liJQU'><center id='liJQU'></center></pre></bdo></b><th id='liJQU'></th></span></q></dt></tr></i><div id='liJQU'><tfoot id='liJQU'></tfoot><dl id='liJQU'><fieldset id='liJQU'></fieldset></dl></div>
            • <small id='liJQU'></small><noframes id='liJQU'>

                  <bdo id='liJQU'></bdo><ul id='liJQU'></ul>

                    <tbody id='liJQU'></tbody>
                  <tfoot id='liJQU'></tfoot>
                • 本文介绍了如何使用 Postman 表单数据在 Django REST Framework 中发布嵌套数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有数组字段的嵌套可写序列化程序.我需要使用表单数据对其进行测试,因为其中一种字段类型是 ImageField.当我将 ImageField 更改为 CharField 时,如果我以原始 JSON 格式发布它,它工作正常.
                  我的简化serializers.py:

                  I have a nested writable serializer with array fields. I need to test it with form-data because one of the field type is ImageField. When I changed the ImageField into CharField it worked fine if I posted it with raw JSON format.
                  My simplified serializers.py:

                  class ProductMarketSerializer(serializers.ModelSerializer):
                      id = serializers.IntegerField(required=False)
                      market = serializers.PrimaryKeyRelatedField(many=False, queryset=Market.objects.all())
                      slabs = serializers.PrimaryKeyRelatedField(many=True, queryset=Slab.objects.all())
                      thicknesses = serializers.PrimaryKeyRelatedField(many=True, queryset=Thickness.objects.all())
                      finish_types = serializers.PrimaryKeyRelatedField(many=True, queryset=FinishType.objects.all())
                  
                      class Meta:
                          model = ProductMarket
                          fields = ['id', 'market', 'name', 'slabs', 'thicknesses', 'finish_types']
                  
                  
                  class ProductSerializer(serializers.ModelSerializer):
                      collection = serializers.PrimaryKeyRelatedField(queryset=ProductCollection.objects.all(), many=False)
                      color = serializers.PrimaryKeyRelatedField(queryset=ColorParent.objects.all(), many=False)
                      images = ProductImageSerializer(many=True)
                      descriptions = ProductDescriptionSerializer(many=True)
                      markets = ProductMarketSerializer(many=True)
                  
                      class Meta:
                          model = Product
                          fields = ['id', 'product_code', 'collection', 'color', 'images', 'descriptions', 'markets', 'video', 'status']
                  
                      def create(self, validated_data):
                          image_data = validated_data.pop('images')
                          description_data = validated_data.pop('descriptions')
                          market_data = validated_data.pop('markets')
                  
                          images = []
                          for image in image_data:
                              img_obj = ProductImage.objects.create(**image)
                              images.append(img_obj)
                  
                          descriptions = []
                          for description in description_data:
                              desc_obj = ProductDescription.objects.create(**description)
                              descriptions.append(desc_obj)
                  
                          markets = []
                          for market in market_data:
                              slabs = market.pop('slabs')
                              thicknesses = market.pop('thicknesses')
                              finish_types = market.pop('finish_types')
                              market_obj = ProductMarket.objects.create(**market)
                              market_obj.slabs.set(slabs)
                              market_obj.thicknesses.set(thicknesses)
                              market_obj.finish_types.set(finish_types)
                              markets.append(market_obj)
                  
                          product = Product.objects.create(**validated_data)
                          product.images.set(images)
                          product.descriptions.set(descriptions)
                          product.markets.set(markets)
                  
                          return product
                  

                  views.py:

                  class ProductView(viewsets.ModelViewSet):
                      permission_classes = [permissions.DjangoModelPermissions]
                      serializer_class = ProductSerializer
                      queryset = Product.objects.all()
                  

                  目前我只能在 1 个 markets 字段中发送 1 个 slabsthicknessesfinish_types.如何为同一个 markets 添加另一个 slabsthicknessesfinish_types?如果我使用表单数据作为正文,正确的键值对格式是什么?

                  创建的Product对象:

                  For now I can only send 1 slabs, thicknesses, and finish_types each within 1 markets field. How to add another slabs, thicknesses, and finish_types for the same markets? What's the correct key-value pair format if I use form-data as Body?

                  The created Product object:

                  {
                      "message": "success",
                      "data": {
                          "id": 60,
                          "product_code": "BQ1010",
                          "collection": 1,
                          "color": 1,
                          "images": [
                              {
                                  "id": 57,
                                  "image": "image",
                                  "default": false
                              }
                          ],
                          "descriptions": [
                              {
                                  "id": 65,
                                  "language": 1,
                                  "description": "new description in english"
                              }
                          ],
                          "markets": [
                              {
                                  "id": 47,
                                  "market": 1,
                                  "name": "White Stone",
                                  "slabs": [
                                      1
                                  ],
                                  "thicknesses": [
                                      2
                                  ],
                                  "finish_types": [
                                      1
                                  ]
                              }
                          ],
                          "video": "https://www.youtube.com",
                          "status": "Continue"
                      }
                  }
                  

                  当我尝试像这样添加第二个 slabsthicknesses 时:
                  slabsthicknesses 字段将为空.

                  When I try to add the second slabs and thicknesses like this:
                  The slabs and thicknesses fields will be empty.

                   "markets": [
                              {
                                  "id": 48,
                                  "market": 1,
                                  "name": "White Stone",
                                  "slabs": [],
                                  "thicknesses": [],
                                  "finish_types": [
                                      1
                                  ]
                              }
                          ],
                  

                  推荐答案

                  如何在 Django REST Framework 中使用 Postman 表单数据发布嵌套数组?

                  group_permission[0]permission:share_item
                  
                  group_permission[0]user_group:1
                  
                  group_permission[0]key:5
                  
                  group_permission[0]groups:1
                  
                  group_permission[0]value:True
                  

                  这篇关于如何使用 Postman 表单数据在 Django REST Framework 中发布嵌套数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 POST 时 Django 中的 MultiValueDictKeyError 下一篇:在 Python 中进行位域操作的最佳方法是什么?

                  相关文章

                    <tfoot id='US8mJ'></tfoot>
                    <legend id='US8mJ'><style id='US8mJ'><dir id='US8mJ'><q id='US8mJ'></q></dir></style></legend>

                      <i id='US8mJ'><tr id='US8mJ'><dt id='US8mJ'><q id='US8mJ'><span id='US8mJ'><b id='US8mJ'><form id='US8mJ'><ins id='US8mJ'></ins><ul id='US8mJ'></ul><sub id='US8mJ'></sub></form><legend id='US8mJ'></legend><bdo id='US8mJ'><pre id='US8mJ'><center id='US8mJ'></center></pre></bdo></b><th id='US8mJ'></th></span></q></dt></tr></i><div id='US8mJ'><tfoot id='US8mJ'></tfoot><dl id='US8mJ'><fieldset id='US8mJ'></fieldset></dl></div>

                    1. <small id='US8mJ'></small><noframes id='US8mJ'>

                        <bdo id='US8mJ'></bdo><ul id='US8mJ'></ul>