Need your help for django rest

A few days ago I was coding a little app, and I’m stuck on something that I can’t solve, and I turned to you, here is my code in serializers.py:

class ImagePublicationSerializer(serializers.ModelSerializer):
    class Meta:
        model = ImagePublication
        fields = ['image']
        extra_kwargs = {
        'publication': {'required': False},
        }
    

class PublicationSerializer(serializers.ModelSerializer):
    video = serializers.FileField(
        max_length=None, use_url=True, required=False)
    proprietary = serializers.ReadOnlyField()
    comments = serializers.ReadOnlyField()
    images = ImagePublicationSerializer(many=True, required=False)
    class Meta:
        model = Publication
        fields = "__all__"

    def create(self, validated_data):        
        product = Publication.objects.create(**validated_data)
        try:
            # try to get and save images (if any)
            images_data = dict((self.context['request'].FILES).lists()).get('images', None)
            for image in images_data:
                ImagePublication.objects.create(publication=product, image=image)
        except:
            # if no images are available - create using default image
            ImagePublication.objects.create(publication=product)
        return product

this code gives me results as follows:

"id": 94,
            "video": null,
            "proprietary": [
                {
                    "id": 19,
                    "password": "pbkdf2_sha256$320000$GorS0VpYVxkwNL8tzJ9AC4$HK2WuRydTUuH8uQcwO7sn9UANbh3zaO5ulvl9Rnf97Q=",
                    "last_login": null,
                    "is_superuser": false,
                    "username": "orthencia",
                    "email": "orthencia2212@gmail.com",
                    "first_name": "Rivoarilala",
                    "last_name": "Orthencia",
                    "is_active": true,
                    "is_staff": false,
                    "date_joined": "2022-05-20 19:54:37.453605+00:00",
                    "birth_date": "1994-07-12",
                    "address": "",
                    "about_me": "I'm english teacher",
                    "color": "dark",
              }
            ],
            "comments": [],
            "images": [
                {
                    "image": "http://localhost:8000/media/publication/ec850a4e9fa3e6204e96ee99b2f0ca90.jpg"
                },
                {
                    "image": "http://localhost:8000/media/publication/NEXTA.png"
                },
                {
                    "image": "http://localhost:8000/media/publication/unnamed.jpg"
                }
            ],
            "message": "teste80",
            "date": "2022-06-21T16:42:00.192163Z",
            "user": 19
        }
    ]
}

my question is how to get just the value without the key like this:

"id": 94,
            "video": null,
            "proprietary": [
                {
                    "id": 19,
                    "password": "pbkdf2_sha256$320000$GorS0VpYVxkwNL8tzJ9AC4$HK2WuRydTUuH8uQcwO7sn9UANbh3zaO5ulvl9Rnf97Q=",
                    "last_login": null,
                    "is_superuser": false,
                    "username": "orthencia",
                    "email": "orthencia2212@gmail.com",
                    "first_name": "Rivoarilala",
                    "last_name": "Orthencia",
                    "is_active": true,
                    "is_staff": false,
                    "date_joined": "2022-05-20 19:54:37.453605+00:00",
                    "birth_date": "1994-07-12",
                    "address": "",
                    "about_me": "I'm english teacher",
                    "color": "dark",
              }
            ],
            "comments": [],
            "images": [
                {
                   "http://localhost:8000/media/publication/ec850a4e9fa3e6204e96ee99b2f0ca90.jpg"
                },
                {
                    "http://localhost:8000/media/publication/NEXTA.png"
                },
                {
                    "http://localhost:8000/media/publication/unnamed.jpg"
                }
            ],
            "message": "teste80",
            "date": "2022-06-21T16:42:00.192163Z",
            "user": 19
        }
    ]
}

I thank you
I precice, I’m new in django

Couple different thoughts here.

First, your “desired” output isn’t valid JSON. The braces - { , by definition, indicate a key-value pair.

Second, while there are people here somewhat familiar with the Django Rest Framework and will try to help you, this isn’t an “officially recognized” support channel for DRF.

Their docs identify a number of places to go for support. You may get better or faster help posting your questions in one of those areas.

1 Like

I wonder if you could use SlugRelatedField. That’s what I typically use when I have a one to many relationship where I want the collection to be an array of a single value. However the fact that it’s an models.ImageField underneath may cause problems.

Otherwise, if that doesn’t work, you could use a SerializerMethodField and return the array from the get_images function. Disclaimer, that’s the hammer and everything looks like a nail approach.