0

I want to save x MediaStreams for one Media Object. So I have a One-to-Many relation here (ForeignKey) but I'm not sure how I can this working as currently always the same stream gets saved and not 5 different ones as expected. Where do I need to place "i" to make this working for object creation within the for-loop?

for i in clean_result['streams']:
    new_stream = MediaStreams.objects.create(index=index_value, stream_bitrate=streambitrate_value,
                                               codec_name=codec_name_value, codec_type=codec_type_value,
                                               width=width_value, height=height_value,
                                               channel_layout=channel_layout_value, language=language_value,
                                               media=new_object)
    new_stream.save()

Currently only and every time index 5 gets saved and not 0-5 - clean_result['streams']['index'].

Thanks in advance

2 Answers2

0

You need to iterate through dict items.

dict_ = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}
>>> for item in dict_.items():
...     print(item)
...
# output will be like this:
('color', 'blue')
('fruit', 'apple')
('pet', 'dog')  

In your case:

for i in clean_result['streams'].items():
    # do your stuff  

If you want to know more about how to iterate through a dictionary in python check out this link.

Mojtaba Arezoomand
  • 2,140
  • 8
  • 23
  • with this snipped I get : AttributeError: 'list' object has no attribute 'item' –  Jun 02 '21 at 09:40
  • i thought you are working with a `dict` as the title of your question says. it means you object is a `list` not a `dict`. you can iterate through a list like `for i in your_list` and it should be fine and if you still have problem it's from somewhere else. – Mojtaba Arezoomand Jun 02 '21 at 10:42
  • I've updated the question accordingly, my bad. –  Jun 02 '21 at 11:19
0

I cleaned your solution, since you are calling objects.create() you do not need to call save(). Also since you are expecting the values inside clean_result['streams'] to exist you can do the following:

for i in clean_result['streams']:
    MediaStreams.objects.create(
        index=i['index'],
        stream_bitrate=i['bit_rate'],
        codec_name=i['codec_name'],
        codec_type=i['codec_type'],
        width=i['width'],
        height=i['height'],
        channel_layout=i['channel_layout'],
        language=i['tags']['language'],
        media=new_object
    )

If there is a chance the values don't exist, you can use i.get('index') which returns none if the index does not exist. See here for dict.get() explaination on StackOverflow.

See here for the Django docs on for objects.create() which calls the save also.

Dean Elliott
  • 1,245
  • 1
  • 8
  • 12
  • One question, all this seems to work fine except for ['tags']['language'] If i want to use i.get this always fails with: ’’’language=i.get['tags']('language'), celery | TypeError: 'builtin_function_or_method' object is not subscriptable’’’ And yes the chance that the field is empty is pratically always given –  Jun 02 '21 at 11:18
  • okay, got it working: language=i.get("tags", {'x': None}).get("language") –  Jun 02 '21 at 11:46