多对多领域中的Django反向关系
我如何得到画廊的回应,如贝娄。现在,galleryserializer只返回带有ID的图像数组的响应。我无法获得图像的详细信息 json响应:多对多领域中的Django反向关系,django,api,django-rest-framework,django-serializer,Django,Api,Django Rest Framework,Django Serializer,我如何得到画廊的回应,如贝娄。现在,galleryserializer只返回带有ID的图像数组的响应。我无法获得图像的详细信息 json响应: { "name": "New Gallery", "images": [ { id: 1, image: 'url/path/to/image', alt_text: 'alt'
{
"name": "New Gallery",
"images": [
{
id: 1,
image: 'url/path/to/image',
alt_text: 'alt'
},
{
id: 2,
image: 'url/path/to/image1',
alt_text: 'alt'
},
]
}
Mymodels.py文件:
class GalleryImage(models.Model):
image = models.ImageField(upload_to='gallery/')
alt_text = models.CharField(max_length=300)
created = models.DateTimeField(auto_now_add=True)
class Gallery(models.Model):
name = models.CharField(max_length=30)
slug = AutoSlugField(populate_from='name', unique_with='id')
images = models.ManyToManyField(GalleryImage, related_name="galleryimages")
created = models.DateTimeField(auto_now_add=True)
class GalleryImageSerializer(serializers.ModelSerializer):
class Meta:
model = GalleryImage
exclude = '__all__'
class GallerySerializer(serializers.ModelSerializer):
class Meta:
model = Gallery
fields = '__all__'
Myserializers.py文件:
class GalleryImage(models.Model):
image = models.ImageField(upload_to='gallery/')
alt_text = models.CharField(max_length=300)
created = models.DateTimeField(auto_now_add=True)
class Gallery(models.Model):
name = models.CharField(max_length=30)
slug = AutoSlugField(populate_from='name', unique_with='id')
images = models.ManyToManyField(GalleryImage, related_name="galleryimages")
created = models.DateTimeField(auto_now_add=True)
class GalleryImageSerializer(serializers.ModelSerializer):
class Meta:
model = GalleryImage
exclude = '__all__'
class GallerySerializer(serializers.ModelSerializer):
class Meta:
model = Gallery
fields = '__all__'
使用嵌套序列化
class GallerySerializer(serializers.ModelSerializer):
images = GalleryImageSerializer(many=True)
class Meta:
model = Gallery
fields = '__all__'
class GallerySerializer(serializers.ModelSerializer):
图像=GalleryImageSerializer(多个=真)
类元:
模型=画廊
fields='\uuuuu all\uuuuu'
现在我得到了一个错误。排除选项必须是列表或元组。得到str.changeexclude='\uuuuuuuuuuu'
到fields='\uuuuuuuuuu'
很高兴这有帮助…请也对答案进行投票:)