我是peewee的新手,目前正尝试从普通的Python SQlite3库进行迁移。
虽然我的代码生成了一个有效的SQL查询,该查询使用SQlite DB浏览器返回了预期的结果,但尝试获取字段的值返回AttributeError: x object has no attribute y
。
型号:
class TableShows(BaseModel):
sonarr_series_id = IntegerField(column_name='sonarrSeriesId', unique=True)
title = TextField()
class Meta:
table_name = 'table_shows'
class TableHistory(BaseModel):
sonarr_series_id = ForeignKeyField(TableShows, field='sonarr_series_id', column_name='sonarrSeriesId')
class Meta:
table_name = 'table_history'
Peewee查询:
data = TableHistory.select(
TableShows.title,
TableHistory.sonarr_series_id
).join(
TableShows
).order_by(
TableShows.title.asc()
)
产生的SQL查询:
SELECT "t1"."title", "t2"."sonarrSeriesId"
FROM "table_history" AS "t2"
INNER JOIN "table_shows" AS "t1" ON ("t2"."sonarrSeriesId" = "t1"."sonarrSeriesId")
ORDER BY "t1"."title" ASC
结果dicts():
{'title': u'Test title', 'sonarr_series_id': 1}
为什么要运行它:
for item in data:
print item.title
返回此:
AttributeError: 'TableHistory' object has no attribute 'title'
答案 0 :(得分:1)
http://docs.peewee-orm.com/en/latest/peewee/relationships.html#selecting-from-multiple-sources
您通过item.sonarr_series_id.title访问数据
您可能会考虑将字段命名为更具Python风格的
。