【问题标题】:Cant perform Error on postgresql database with peewee (Python) : IndexError: tuple index out of range无法使用 peewee (Python) 在 postgresql 数据库上执行错误:IndexError: tuple index out of range
【发布时间】:2018-05-13 11:11:05
【问题描述】:

我正在用 FLASK 试用 Peewee ORM。 我创建了两个模型:作者和文章。一位作者有_许多文章。因此表 article 有一个外键 author_id。

这是我的模型:

class BaseModel(Model):
    class Meta:
        database = psql_db
class Article(BaseModel):
    id = PrimaryKeyField(null=False)
    author_id = ForeignKeyField(Author, backref='articles', null=True)
    headline = TextField(null=True)
    snippet = TextField(null=True)
    publication_date = DateTimeField(null=True)
    nytimes_id = CharField(max_length=100, unique=True)



@property
def serialize(self):
    data = {
        'id': self.id,
        'headline': str(self.headline).strip(),
        'snippet': str(self.snippet).strip(),
        'publication_date': str(self.publication_date).strip(),
    }

    return data

def __repr__(self):
    return "{}, {}, {}, {}, {}".format(
        self.id,
        self.headline,
        self.snippet,
        self.publication_date
    )

```

class BaseModel(Model):
    class Meta:
        database = psql_db

class Author(BaseModel):
    id = PrimaryKeyField(null=False)
    full_name = CharField(max_length=100)
    description = TextField(null=True)

然后我在我的烧瓶 api 中有一个端点,我在其中发布文章 ID 和作者 ID。我尝试获取文章和作者并将它们关联:

@app.route('/api/v1/associate-article-author', methods=['POST'])
def associate_author_article():
    author_id = request.json['author_id']
    article_nyId = request.json['selected_article_nyid']
    author = Author.select().where(Author.id == author_id).get()
    article = Article.select().where(Article.nytimes_id == article_nyId).get()
    article.author = author
    article.save()
    return json.dumps({'success':True}), 200, {'ContentType':'application/json'}

但是Article.select().where(Article.nytimes_id == article_nyId).get()Author.select().where(Author.id == author_id).get() 总是提高IndexError: tuple index out of range。 我不明白这个错误信息或我在这里做错了什么。

这是完整的回溯:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/davidgeismar/code/davidgeismar/react-app/nytimes-api/api.py", line 62, in associate_author_article
    print(article)
  File "/Users/davidgeismar/code/davidgeismar/react-app/nytimes-api/models/article.py", line 39, in __repr__
    self.publication_date
IndexError: tuple index out of range

【问题讨论】:

    标签: python postgresql flask peewee


    【解决方案1】:

    问题出在Article.__repr__

    def __repr__(self):
        return "{}, {}, {}, {}, {}".format(
            self.id,
            self.headline,
            self.snippet,
            self.publication_date
        )
    

    格式字符串包含五个占位符,但只提供了四个值,因此是IndexError

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 2021-10-27
      相关资源
      最近更新 更多