Let's suppose that my django rest framework app is returning this json for this url (where 4 is the pk of a model): http:mydomain.com/model_number/4/?format=json
[
{
"id": 96,
"title": "title 1",
"picture": "static/uploads/pic1.jpg"
},
{
"id": 97,
"title": "title 2",
"picture": "static/uploads/pic2.jpg"
},
{
"id": 98,
"title": "title 3",
"picture": "static/uploads/pic3.jpg"
}
]
I want to create and download a zip with a json file which has the content explained above (generated by django rest framework), and a folder with pic1
, pic2
and pic3
.
I'm able to download a zip with a folder containing those pictures, but I don't know how to include the json file.
I don't know if it is related with parsers :
Any help would be appreciated
Just construct an instance of your serializer with model instance as argument. Serializer object has property data
.
class YourModel(models.Model):
...
class YourModelSerializer(serializers.ModelSerializer):
...
your_json = YourModelSerializer(YourModel.objects.get(pk=5)).data
You also can use queryset with flag many=True
:
your_json = YourModelSerializer(YourModel.objects.all(), many=True).data
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.