我有这样的模型:
class AbstractBaseModel(peewee.Model):
uuid = peewee.UUIDField(default=uuid4)
is_active = peewee.BooleanField(default=False)
is_deleted = peewee.BooleanField(default=False)
created_at = peewee.DateTimeField(default=datetime.now)
updated_at = peewee.DateTimeField(default=datetime.now)
deleted_at = peewee.DateTimeField(null=True)
class Meta:
abstract = True
indexes = (
(('is_active', 'is_deleted'), False),
)
我想:
is_active == True
和is_deleted == False
的行或
is_active
和is_deleted
标志答案 0 :(得分:1)
这只是Python ...向模型类添加一个辅助方法。例如,如果我有一个Post
类博客文章,我可能会写:
class Post(Model):
status = IntegerField() # 1=live, 2=draft, 3=deleted, e.g.
@classmethod
def visible(cls):
return cls.select().where(cls.status == 1)
然后,我可能会看到所有已发布帖子的列表视图:
@app.route('/')
def post_index():
query = Post.visible().order_by(Post.timestamp.desc())
return render_template('post_index.html', query=query)
答案 1 :(得分:0)
这是皮尤人所不具备的答案。
假设您上面的(未命名)表是“事物”,那么从本质上讲,您的模型很适合select * from thing
之类的查询。
现在定义一个相关视图,以选择某些内容,并选择您喜欢的行子集:
sqlite> create view thing_v as
...> select uuid, created_at, updated_at, deleted_at
...> from thing
...> where is_active == True and is_deleted == False;
将模型指向那个关系,该关系恰好是视图(命名查询)而不是表。
答案 2 :(得分:0)
我想提供很好的coleifer答案。
对于联接和更复杂的查询,我们可以使用PeeWee CTE功能,如下所示:
class BaseModel(peewee.Model):
is_deleted = peewee.BooleanField(default=False, index=True)
@classmethod
def visible_where_clause(cls):
return cls.is_deleted == False
@classmethod
def visible(cls):
return cls.select().where(cls.visible_where_clause())
@classmethod
def cte(cls, name=None):
if name is None:
name = 'CTE_{}'.format(cls.__name__)
return cls.visible().cte(name)
class Meta:
abstract = True
class User(BaseModel):
username = peewee.CharField(max_length=255, unique=True)
is_active = peewee.BooleanField(default=True, index=True)
@classmethod
def visible_where_clause(cls):
return (
(super().visible_where_clause()) & (cls.is_active == True)
)
UserCTE = User.cte()
class Post(BaseModel):
status = peewee.IntegerField() # 1=live, 2=draft, 3=deleted, e.g.
user = peewee.ForeignKeyField(User)
title = peewee.CharField(max_length=255)
@classmethod
def visible_where_clause(cls):
return (
(super().visible_where_clause()) & (cls.status == 1)
)
PostCTE = Post.cte()
然后,当我们只希望活跃用户的活跃帖子时,我们可以执行以下操作:
posts = (
Post
.visible()
.select(Post.title)
.join(UserCTE, on=Post.user == UserCTE.c.id)
.with_cte(UserCTE)
)
for post in posts:
print(post.title)
记住
您必须通过添加CTE
来引用.c.<column_name>
对象,并在查询结束时通过添加cte
来添加此.with_cte(<list of all used CTEs>)