(python) peewee的related_name是做什么用的

from peewee import *
from pprint import pprint
import datetime
import random
import sys
db = SqliteDatabasepeople.db class UserModel: uid = IntegerFieldprimary_key=True name = CharField class Meta: database = db # This model uses the "people.db" database.
class TweetModel: user = ForeignKeyFieldUser, related_name=tweets message = TextField created_date = DateTimeFielddefault=datetime.datetime.now is_published = BooleanFielddefault=True
#db.create_tables[User,Tweet]
username=strrandom.random+"oo"
msg=strrandom.random+"xx"
try: user = User.getname=username printuser.uid
except: user=User.createname=username printuser.uid try: tt = Tweet.getuser=4 printtt.user
except: user=Tweet.createuser=4,message=msg printuser.message total=User.select.count
printtotal
t=Tweet.select.joinUser.whereUser.uid ==8.order_byTweet.created_date.desc.get
printt

http://peewee.readthedocs.org/en/latest/peewee/querying.html
照着官方的文档写的,但是报错
peewee.OperationalError: no such table: user
user表肯定有,因为create,get都能正确执行
就是到join关联表的时候出错
写成

t=Tweet.select.jointweets.whereUser.uid ==8.order_byTweet.created_date.desc.get

也一样

就是反向引用名称。你可以用user.tweets访问一个用户的tweet列表

发表评论

电子邮件地址不会被公开。 必填项已用*标注