【发布时间】:2023-01-30 06:30:48
【问题描述】:
我正在尝试在 python django 的不同文件中编辑列表。我有一个名为 models.py 的文件和一个名为 details.py 的文件,
细节.py:
DATA = [
{'height': '184', 'width': '49'},
{'height': '161', 'width': '31'},
{'height': '197', 'width': '25'},
{'height': '123', 'width': '56'},
{'height': '152', 'width': '24'},
{'height': '177', 'width': '27'},
]
def edit_list(h,w):
for info in DATA:
if info['height'] == h:
info['width'] = w
return True
models.py:
from abc.details import edit_list
height = '161'
new_width = '52'
update_data = edit_list(height, new_width) #this doesn't work, when I check the file nothing changes in the list :/
使这成为可能的最佳方法是什么?
(我不想将此列表导入数据库并只更新那里的宽度,我希望宽度在文件本身内部更新,删除 details.py 文件并在无法进行编辑时使用 python 创建一个新文件因为很少有其他函数也一直从列表中获取数据。
【问题讨论】: