我正在尝试在我的 python 应用程序中实现 peewee,并且在定义我的类时如下所示:
import datetime
import peewee as pw
import acme.core as acme
adapter = pw.MySQLDatabase(
acme.get_config(path='database.db'),
host=acme.get_config(path='database.host'),
port=int(acme.get_config(path='database.port', default=3306)),
user=acme.get_config(path='database.user'),
passwd=acme.get_config(path='database.password'))
class Model(pw.Model):
"""
The base model that will connect the database
"""
id = pw.PrimaryKeyField()
created_at = pw.DateTimeField()
updated_at = pw.DateTimeField(default=datetime.datetime.now)
class Meta:
database = adapter
class ServerModule(Model):
enabled = pw.BooleanField()
ipaddr = pw.IntegerField()
port = pw.IntegerField()
class Meta(Model.Meta):
db_table = 'module_server'
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Traceback (most recent call last):
File "db.py", line 25, in <module>
class ServerModule(Model):
File …Run Code Online (Sandbox Code Playgroud) 我在 Peewee 中有以下简单模型:
class SiteText(BaseModel):
url = TextField()
text = TextField()
my_counter = IntegerField()
def get_text_by_url(url):
d = [s.text for s in SiteText.select(SiteText.url == url)]
d = d[0] if len(d) > 0 else None
return d
def save_text(updates):
# updates is a dict containing, url, text, my_counter
SiteText.upsert(**updates)
def get_outage_counter(url):
c = [c.my_counter for c in SiteText.select(SiteText.url == url)]
c = c[0] if len(c) > 0 else None
return c
def set_outage_counter(url, counter):
c = SiteText.get(SiteText.url == url)
c.counter = …Run Code Online (Sandbox Code Playgroud) Python 中有没有一种简单的方法可以从 SQLite 逐表迁移到 Postgres?我正在使用 Peewee,可以生成 SQLite 转储,但没有可用于导入到 Postgres 的 psql。Python 中有一个包可以实现这个功能吗?我还使用 Peewee 作为 ORM,并且涵盖了生成模式位,问题是数据。
数据库有点大,1.3gb
我正在使用peewee2.1与python3.3和sqlite3.7数据库.
我想执行某些SELECT查询,其中:
我的数据库有一个'Event'表,每个事件有1条记录,还有一个'Ticket'表,每个事件有1..N票.每个故障单记录都包含事件的id作为外键.每张票还包含一个"席位"列,用于指定购买的座位数.("票"最好被认为是活动中一个或多个席位的购买交易.)
下面是两个使用这种SQLite查询的例子,它给了我想要的结果:
SELECT ev_tix, count(1) AS ev_tix_n FROM
(SELECT count(1) AS ev_tix FROM ticket GROUP BY event_id)
GROUP BY ev_tix
SELECT seat_tot, count(1) AS seat_tot_n FROM
(SELECT sum(seats) AS seat_tot FROM ticket GROUP BY event_id)
GROUP BY seat_tot
Run Code Online (Sandbox Code Playgroud)
但是使用Peewee,我不知道在指定外部查询时如何选择内部查询的聚合(计数或总和).我当然可以为该聚合指定一个别名,但似乎我不能在外部查询中使用该别名.
我知道Peewee有一个执行"原始"SQL查询的机制,我已经成功地使用了这种解决方法.但我想了解是否/如何使用Peewee直接完成这些查询.
可以说我有
import peewee
class Foo(Model):
name = CharField()
Run Code Online (Sandbox Code Playgroud)
我想做以下事情:
f = {id:1, name:"bar"}
foo = Foo.create_from_dict(f)
Run Code Online (Sandbox Code Playgroud)
这是Peewee的本地人吗?我无法在源代码中发现任何内容.
我已经编写了这个函数,但是如果存在则宁可使用本机函数:
#clazz is a string for the name of the Model, i.e. 'Foo'
def model_from_dict(clazz, dictionary):
#convert the string into the actual model class
clazz = reduce(getattr, clazz.split("."), sys.modules[__name__])
model = clazz()
for key in dictionary.keys():
#set the attributes of the model
model.__dict__['_data'][key] = dictionary[key]
return model
Run Code Online (Sandbox Code Playgroud)
我有一个显示所有foos的网页,允许用户编辑它们.我希望能够将JSON字符串传递给控制器,在那里我将它转换为dict,然后从中制作Foos,因此我可以根据需要进行更新.
我真的很享受Peewee ORM.它重量轻,易于使用,并且记录良好.我抓麻烦的一件事是related_name实现外键时使用的属性.我永远不确定该属性是否应该与表或列相关.有人可以向我解释我应该使用的名称,以及如何使用?例如,在Peeewee文档中找到了Student/Courses示例.
https://peewee.readthedocs.org/en/2.0.2/peewee/fields.html#implementing-many-to-many
class Student(Model):
name = CharField()
class Course(Model):
name = CharField()
class StudentCourse(Model):
student = ForeignKeyField(Student)
course = ForeignKeyField(Course)
Run Code Online (Sandbox Code Playgroud)
假设我有学生,课程,学生课程模型.StudentCourse专栏的相关名称是什么?
由于Peewee中的"删除"查询不允许加入,删除table_2中与相关table_1中的特定条件匹配的所有记录的最佳方法是什么?
使用一个简单的例子,我想实现相当于:
DELETE message.*
FROM message
JOIN user ON message.from_user_id = user.id
WHERE user.name = "Joe";
Run Code Online (Sandbox Code Playgroud) 有没有办法在peewee中定义自动增量场.
我知道我们可以定义序列,但需要手动创建序列而不是由create_tables管理,这阻止了我使用它.(构建过程由create tables管理,我宁愿不添加手动步骤)
import peewee
class TestModel(peewee.Model):
test_id = peewee.BigIntegerField(sequence='test_id_seq')
Run Code Online (Sandbox Code Playgroud)
替代上面的代码,我宁愿拥有.由于大多数数据库都有串行字段,因此我没有看到维持序列的要点.
import peewee
class TestModel(peewee.Model):
test_id = peewee.AutoIncremenetIntField()
Run Code Online (Sandbox Code Playgroud) 我想根据用户名或电子邮件地址为用户运行查询.
我一定是想念它,但我找不到如何OR在peewee文档中运行查询.你是怎样做的?
考虑从SQLAlchemy切换到peewee,但有一个基本问题,因为我无法找到这样的例子.我想执行一个返回匹配对象列表的查询.什么有效是获取哪个返回单个记录:
Topping.select().where(Topping.id==jalapenos.id).get()
Run Code Online (Sandbox Code Playgroud)
我想得到的是一个结果列表,所有示例都表明我应该迭代.有没有办法从以下结果中获取结果列表:
Topping.select(Topping).where(Topping.stock > 0)
Run Code Online (Sandbox Code Playgroud) peewee ×10
python ×7
sqlite ×2
orm ×1
postgresql ×1
python-2.7 ×1
python-3.x ×1
sql ×1
sql-delete ×1