I want to return a nested representation of Customer with Image objects, where a Customer can have many Images.
Currently, the GET request returns a list of ALL images from the Image queryset for each Customer object as shown below.
How can I show only related Image(s) for each Customer object in a list instead?
# The 'images' field currently returns all images rather than related images to the customer id.
[
{
'id': 1,
'name': 'John Doe',
'images': [
{'id': 1, 'name': 'foo.jpg', 'customer': 1},
{'id': 2, 'name': 'bar.jpg', 'customer': 2},
{'id': 3, 'name': 'foobar.jpg', 'customer': 3},
...
]
},
{
'id': 2,
'name': 'Jane Doe',
'images': [
{'id': 1, 'name': 'foo.jpg', 'customer': 1},
{'id': 2, 'name': 'bar.jpg', 'customer': 2},
{'id': 3, 'name': 'foobar.jpg', 'customer': 3},
...
]
},
...
]
This is my current setup
Example Models
class Customer(models.Model):
name = models.CharField()
class Image(models.Model):
name = models.CharField()
customer = models.ForeignKey(
Customer, on_delete=models.CASCADE)
Example Serializers
class CustomerSerializer(serializers.ModelSerializer):
# My Customer nested relationship is expressed using ImageSerializer as a field
images = ImageSerializer(many=True)
class Meta:
model = Customer
fields = ('id', 'name', 'images')
read_only_fields = ('id',)
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = '__all__'
read_only_fields = ('id',)
Please let me know if my question is unclear and I will update my question. Thank you.