Home > Software design >  Get multipe images urls on Django RestFramework
Get multipe images urls on Django RestFramework

Time:11-03

I'm using Django RestFramework to create a simple eCommerce API where one product could have many images and I would like to get the URLs of all these images on a json field.

For now, I got the first image url using "imagembicicleta_set.all.first.image.url" on the serializer, but I need all URLs list:

{
    "count": 7,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "nome": "Specialized Roubaix",
            "marca__nome": "Specialized",
            "categoria": "Bicicletas de Estrada",
            "atividades": [
                1
            ],
            "terrenos": [
                "Asfalto"
            ],
            "preco": "16999.00",
            "ano": 1,
            "absolute_url": "/bicicletas/Specialized/specialized-roubaix-2020/",
            "img_url": "/media/images/bicicletas/roubaix1.jpeg"
        },
        {
            "id": 2,
            "nome": "Specialized Roubaix Sport",
            "marca__nome": "Specialized",

Following how is my setup:

Models.py

class Bicicleta(models.Model):
    id = models.AutoField(primary_key=True)
    nome = models.CharField(max_length=200, blank=False, null=False)
    slug = models.SlugField(unique=True)
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, blank=False, null=False)
    descricao = RichTextField(blank=True, null=True)
    marca = models.ForeignKey(MarcaBicicleta, blank=True, null=True, on_delete=models.SET_NULL)
    ...

class ImagemBicicleta (models.Model):
    bicicleta = models.ForeignKey(Bicicleta, default=None, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='images/bicicletas')

Serializer.py

class BicicletasSerializer(serializers.ModelSerializer):
    marca__nome = serializers.CharField(source='marca.nome')
    categoria = serializers.CharField(source='categoria.nome')
    terrenos = serializers.StringRelatedField(many=True)
    absolute_url = serializers.URLField(source='get_absolute_url', read_only=True)
    img_url = serializers.URLField(source='imagembicicleta_set.all.first.image.url', read_only=True) #I could get the first image using this
    class Meta:
        model = Bicicleta
        fields = ['id', 'nome', 'marca__nome', 'categoria', 'atividades', 'terrenos', 'preco', 'ano', 'absolute_url', 'img_url']

views.py

class BicicletasView(generics.ListAPIView):
    serializer_class = BicicletasSerializer
    queryset = Bicicleta.objects.all()
    filter_backends = (DjangoFilterBackend, SearchFilter)
    filterset_fields = ['marca', 'terrenos', 'status']
    search_fields = {'nome': ['icontains'], }

How could I get all images URLs in the field?

Per example, if a product has 3 different images, I would expect to have the img field like this:

"img_url": [ "/media/images/bicicletas/roubaix1.jpeg","/media/images/bicicletas/roubaix2.jpeg","/media/images/bicicletas/roubaix3.jpeg" ],

CodePudding user response:

You can add a method serializer which will collect all the urls for each individual object like this:

class BicicletasSerializer(serializers.ModelSerializer):
    marca__nome = serializers.CharField(source='marca.nome')
    categoria = serializers.CharField(source='categoria.nome')
    terrenos = serializers.StringRelatedField(many=True)
    absolute_url = serializers.URLField(source='get_absolute_url', read_only=True)
    img_url = serializers.SerializerMethodField()

    def get_image_url(self , instance):
        return ImagemBicicleta.objects.filter(bicicleta=instance).values_list('image',flat=True)
 
    class Meta:
        model = Bicicleta
        fields = ['id', 'nome', 'marca__nome', 'categoria', 'atividades', 'terrenos', 'preco', 'ano', 'absolute_url', 'img_url']

  • Related