简体   繁体   中英

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()
Someone can help ?

get_or_create() returns a tuple. You do not have to explicitly call save()

Use

label, created = Label.get_or_create(name=subdirname)
  • created will return if a new object is saved to DB
  • label will return you the object that was saved.

MoreInfo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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