我想为一个媒体对象保存x个媒体流。所以我在这里有一个一对多的关系(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时-清除结果['streams']['index']
提前谢谢
我清理了您的解决方案,因为您正在调用
objects.create()
,所以不需要调用save()
。另外,由于您希望clean_result['streams']
中的值存在,因此可以执行以下操作:如果这些值可能不存在,则可以使用
i.get('index')
,如果index
不存在,则返回none
。有关StackOverflow的解释,请参见here请参见here,了解
objects.create()
上的Django文档,它也调用save您需要遍历dict项
就你而言:
如果您想了解更多关于如何在python中迭代字典的信息,请查看此link
让它像这样工作:
相关问题 更多 >
编程相关推荐