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