我有一个函数:
podcast_instance = Show.objects.get_or_create(title=parsed_podcast.title
day_published=parsed_podcast.day_published )它从一个传入的对象(parsed_podcast)中获取数据。它有大量的属性,根据播客的不同,一些属性存在,一些属性不存在。我有很多数据源被传递给'Podcast‘对象,所以我想让我的函数保存可用的数据,如果没有可用的,或者'parsed_podcast object没有属性x’,那么只需将' nothing‘保存到该属性中并继续。model Show允许这些属性没有值。
但是,get_or_create只是抛出一个AttributeError并停止,如果我将它放在一个Try块中,并以这种方式打印错误:
try:
podcast_instance = Show.objects.get_or_create(title=parsed_podcast.title)
except AttributeError:
Exception它仍然不保存数据。如何创建一个对象并只保存可用的内容?
发布于 2019-04-17 20:55:39
AttributeError是因为您访问的是由get_or_create返回的元组,而不是对象。
使用:
podcast_instance, created = Show.objects.get_or_create(title=parsed_podcast.title
day_published=parsed_podcast.day_published)你就可以走了。docs
https://stackoverflow.com/questions/55722598
复制相似问题