我有一个带数组字段的嵌套可写序列化程序。我需要用form-data测试它,因为其中一个字段类型是ImageField
。当我将ImageField
更改为CharField
时,如果我以原始JSON格式发布它,它就可以正常工作。
我的简化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个slabs
、thicknesses
和finish_types
。如何为同一个markets
添加另一个slabs
、thicknesses
和finish_types
?如果我使用form-data作为Body,正确的键值对格式是什么?
x1c 0d1x的数据
创建的Product
对象:
{
"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"
}
}
型
当我尝试像这样添加第二个slabs
和thicknesses
时:
的slabs
和thicknesses
字段将为空。
"markets": [
{
"id": 48,
"market": 1,
"name": "White Stone",
"slabs": [],
"thicknesses": [],
"finish_types": [
1
]
}
],
型
3条答案
按热度按时间pbgvytdp1#
如何在Django REST Framework中使用Postman form-data发布嵌套数组?
x1c 0d1x的数据
字符串
qyswt5oh2#
截至2023年8月4日:现在,您可以仅使用点表示法给予嵌套值。不需要[0]。示例:
字符串
注意:对于Django用户,你需要为你的视图设置一个解析器类。
例如:
第一个月
jjjwad0x3#
多集Python
字符串