Display
和Create
序列化器。.models.py
class Material(models.Model):
name = models.CharField(max_length=120)
class Product(models.Model):
name = models.CharField(max_length=120)
materials = models.ManyToManyField(Material, through='MaterialProduct')
class MaterialProduct(models.Model):
material = models.ForeignKey(Material, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
rate = models.FloatField(default=100)
views.py
class Products(APIView):
def get(self, request, format=None):
products = Product.objects.all()
serializer = ProductDisplaySerializer(products, many=True) # Display
return Response(serializer.data)
def post(self, request, format=None):
serializer = ProductCreateSerializer(data=request.data) # Create
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializers.py
class MaterialSerializer(serializers.ModelSerializer):
class Meta:
model = Material
fields = '__all__'
class ProductMaterialRateSerializer(serializers.ModelSerializer):
material = MaterialSerializer(read_only=True)
material_id = serializers.PrimaryKeyRelatedField(
write_only=True, source='material', queryset=Material.objects.all())
class Meta:
model = MaterialProduct # attention!!!
fields = ('material', 'material_id', 'rate')
class ProductCreateSerializer(serializers.ModelSerializer):
'''To create a product with existed material and a material rate(extra field) '''
materials = ProductMaterialRateSerializer(many=True)
class Meta:
model = Product
fields = ('id', 'name', 'materials')
def create(self, validated_data):
materials_data = validated_data.pop('materials')
product = Product.objects.create(**validated_data)
for material_data in materials_data:
MaterialProduct.objects.create(
product=product,
material=material_data.get('material'),
rate=material_data.get('rate'))
return product
{
"id": 29,
"name": "product 4",
"materials": [
{
"material_id": 3,
"rate": 30
},
{
"material_id": 2,
"rate": 70
}
]
}
{
"name" : "product 4",
"materials" : [
{
"material_id":3,
"rate" : 30
}
,{
"material_id":2,
"rate" : 70
}
]
}
{
"id": 29,
"name": "product 4",
"materials": [
{},
{}
]
}
答案 0 :(得分:1)
您可以使用RetrieveAPIView
进行显示,并使用CreateAPIView
进行创建,这是内置功能,可以使事情变得更简单,并使其他开发人员更轻松地使用您的代码,因为他们所需要的只是DRF的工作方式与您编写代码的方式不同。基本上,最好遵循其他开发人员熟悉的模式。
我不建议组合创建和显示序列化程序,除非它们真的很简单。如果它们变得有些复杂,特别是在其他人正在调试您的代码的情况下,将可能难以调试或进行更改!
对于序列化m2m,materials = ProductMaterialRateSerializer(many=True)
是正确的方法,创建时可以使用内置功能,但是如果它很复杂并且需要一些计算,那么我建议覆盖save
和update
创建序列化程序中的方法。
以及返回的数据:
您可以像这样覆盖上下文:
def post(self, request, format=None):
serializer = ProductCreateSerializer(data=request.data) # Create
if serializer.is_valid():
# Get the saved object
saved_obj = serializer.save()
# Serialize the saved object with the preferred serializer.
response_data = ProductDisplaySerializer(saved_obj).data
return Response(response_data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)