我想要一个可以在create(post)上传多个图像的模型。
在api / animals /上的DRF Web视图中,以邮政表格的形式,我想添加多个图像并创建带有图像的新动物。
让我们假设我具有以下模型:
class Animal(models.Model):
slug = models.CharField(max_length=20, unique=True)
class AnimalImage(models.Model):
animal = models.ForeignKey(Animal, on_delete=models.CASCADE)
image = models.ImageField(upload_to='animal_pics/')
我有以下序列化器:
class AnimalImageSerializer(serializers.ModelSerializer):
class Meta:
model = AnimalImage
fields = ('animal', 'image', )
class AnimalSerializer(serializers.HyperlinkedModelSerializer):
images = AnimalImageSerializer(many=True)
class Meta:
model = Animal
lookup_field = 'slug'
extra_kwargs = {
{'url': {'lookup_field': 'slug'}
}
fields = ('slug', 'images', )
我有以下休息视图:
class AnimalViewSet(viewsets.ModelViewSet):
queryset = Animal.objects.all()
serializer_class = AnimalSerializer
lookup_filed = 'slug'
parser_classes = (JSONParser, MultiPartParser, FormParser)
当我使用drf网络界面时: