copser
copser

Reputation: 2641

get multiple value from json and save them in model

In json I have a bunch of bullets like this

{'Bullet 1': 'Made from high gloss polypropylene', 'Bullet 2': 'Card pocket for personalisation on front of wallet', 'Bullet 3': 'Additional pocket on rear of wallet', 'Bullet 4': 'Stud closure to keep contents secure', 'Bullet 5': 'Colour: Blue', 'Bullet 6':}

After iteration and cleanup of the json I want to save all Bullet's in one model filed, right now I'm saving only one

for _product in json_obj:
    Product.objects.update_or_create(
        ...
        # save all BUllet ???
        bullet=_product['Bullet 1'],
        ...
     )

How can I fetch all of them and save them into my model field?

Upvotes: 0

Views: 16

Answers (1)

Carlo 1585
Carlo 1585

Reputation: 1477

If you need pass 1 value each iteration you can do the follow, if you need give to Product.objects.update_or_create() a list of values or more values all together the code change a bit.

json_obj = {'Bullet 1': 'Made from high gloss polypropylene', 'Bullet 2': 'Card pocket for personalisation on front of wallet', 'Bullet 3': 'Additional pocket on rear of wallet', 'Bullet 4': 'Stud closure to keep contents secure', 'Bullet 5': 'Colour: Blue', 'Bullet 6':'test'}
for i in json_obj .keys():
    print json_obj [i]
    Product.objects.update_or_create(json_obj [i])

I printed for let u see what get in each loop:

Made from high gloss polypropylene
Card pocket for personalisation on front of wallet
Additional pocket on rear of wallet
Stud closure to keep contents secure
Colour: Blue
test

Upvotes: 1

Related Questions