【问题标题】:Django how to save objects in a for loop from listDjango如何将对象保存在列表中的for循环中
【发布时间】:2021-06-02 08:43:06
【问题描述】:

我想为一个媒体对象保存 x 个 MediaStreams。所以我在这里有一个一对多的关系(ForeignKey),但我不确定我如何才能做到这一点,因为目前总是保存相同的流,而不是预期的 5 个不同的流。我需要在哪里放置“i”才能在 for 循环中创建对象?

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()

目前只保存索引 5 而不是 0-5 - clean_result['streams']['index']。

提前致谢

【问题讨论】:

  • 你能添加堆栈跟踪吗?
  • @DeanElliott 对不起,但现在我没有这个问题了,所以没有堆栈跟踪......我仍然无法通过 dict 循环。

标签: python django for-loop model


【解决方案1】:

我清理了您的解决方案,因为您调用的是 objects.create(),所以您不需要调用 save()。此外,由于您期望 clean_result['streams'] 中的值存在,您可以执行以下操作:

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
    )

如果值不存在,您可以使用i.get('index'),如果index 不存在,则返回none。有关 StackOverflow 上的 dict.get() 说明,请参阅 here

请参阅 here 上的 Django 文档,了解 objects.create() 也调用保存。

【讨论】:

  • 一个问题,除了 ['tags']['language'] 如果我想使用 i.get 这总是失败:'''language=i.get ['tags']('language'), 芹菜 | TypeError: 'builtin_function_or_method' object is not subscriptable''' 是的,该字段为空的机会实际上总是给定的
  • 好的,开始工作了:language=i.get("tags", {'x': None}).get("language")
【解决方案2】:

您需要遍历 dict 项。

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')  

在你的情况下:

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

如果您想了解更多关于如何在 python 中遍历字典的信息,请查看link

【讨论】:

  • 用这个剪断我得到: AttributeError: 'list' object has no attribute 'item'
  • 我以为您正在使用dict,正如您问题的标题所说。这意味着您反对的是list 而不是dict。您可以遍历 for i in your_list 之类的列表,它应该没问题,如果您仍然有问题,它来自其他地方。
  • 我已经相应地更新了问题,我的错。
【解决方案3】:

让它像这样工作:

for i in clean_result['streams']:
    if 'index' in i:
        index_value = i['index']
    if 'bit_rate' in i:
        streambitrate_value = i['bit_rate']
    if 'codec_name' in i:
        codec_name_value = i['codec_name']
    if 'codec_type' in i:
        codec_type_value = i['codec_type']
    if 'width' in i:
        width_value = i['width']
    if 'height' in i:
        height_value = i['height']
    if 'channel_layout' in i:
        channel_layout_value = i['channel_layout']
    if 'tags' in i and 'language' in i['tags']:
        language_value = i['tags']['language']
    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()

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多