I'm creating a dictionary in python. I'm getting same objects stored in dictionary while I loop through object. Where am I doing wrong?
I tried dict(), however, I'm avoiding dict().
Code I've tried is here:
image_dict = {}
#query for getting objects
images = Image_history.objects.all()
for image in images:
image_history = dict({
"type": image.image_type,
"timestamp": image.last_updated_timestamp,
})
image_dict.append(image_history)
My problem is when I use this following method to create dictionary in python:
image_dict = {}
image_list = {}
# list of image_details
image_list["image_details"] = []
#query for getting objects
images = Image_history.objects.all()
#looping through object and storing in dictionary
for image in images:
image_dict['type']= image.image_type
image_dict['timestamp']= image.last_updated_timestamp
#appending all those objects in loop to image_list
image_list["image_details"].append(image_dict)
I expect the output to be a list of different objects. But, I'm getting list of same duplicate objects. Expected output:
{
"image_detail":[
{
"type": "png",
"timestamp": "1-12-18"
},
{
"type": "jpeg",
"timestamp": "1-1-19"
}
]
}
Actual output I'm getting:
{
"image_detail":[
{
"type": "jpeg",
"timestamp": "1-1-19"
},{
"type": "jpeg",
"timestamp": "1-1-19"
}
]
}