简体   繁体   English

如何从Python中的元组对象保存数据?

[英]How to save data from a tuple object in Python?

I am facing this error: 我遇到此错误:

AttributeError: 'tuple' object has no attribute 'save'

from the following piece of code: 从以下代码中:

def load_images_to_db(path):
  for dirname, dirnames, filenames in os.walk(path):
    for subdirname in dirnames:
      subject_path = os.path.join(dirname, subdirname)
      label = Label.get_or_create(name=subdirname)
      label.save()

The error comes from this line: label.save() 错误来自此行: label.save()
Someone can help ? 有人可以帮忙吗?

get_or_create() returns a tuple. get_or_create()返回一个元组。 You do not have to explicitly call save() 您不必显式调用save()

Use 采用

label, created = Label.get_or_create(name=subdirname)
  • created will return if a new object is saved to DB 如果将新对象保存到数据库,则created将返回
  • label will return you the object that was saved. label将返回保存的对象。

MoreInfo 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM