显示数据库中布尔值的计数
Display a count of Booleans from database
我正在使用烧瓶。数据库是sqlite3。
简而言之。我可以制作列表,我可以将它们勾选为 "finished",我想在用户配置文件中显示完成列表的数量。
此时显示与"all lists"相同..
这是我的列表模型:
class List(Model):
timestamp = DateTimeField(default=datetime.datetime.now)
user = ForeignKeyField(
rel_model=User,
related_name='list'
)
content = TextField()
finished = BooleanField(default=False)
class Meta:
database = DATABASE
order_by = ('-timestamp',)
这是我在用户模型上的查找完成列表函数:
def finished_lists(self):
return List.select().where(
(List.user == self) |
(List.finished == True)
)
这是个人资料模板:
{% extends "stream.html" %}
{% block content %}
<div class="row">
<div class="">
<h1>{{ user.username }}</h1>
</div>
<div class="">
<div class="">
<h5>Followers</h5>
<p>{{ user.followers().count() }}</p>
</div>
<div class="">
<h5>Following</h5>
<p>{{ user.following().count() }}</p>
</div>
<div class="">
<h5>Lists</h5>
<p>{{ user.list.count() }}</p>
</div>
<div class="">
<h5>Finished</h5>
<p>{{ user.finished_lists().count() }}</p>
</div>
</div>
<div class="grid-25">
{% if current_user.is_authenticated %}
{% if user != current_user %}
{% if not user in current_user.following() %}
<a href="{{ url_for('follow', username=user.username) }}" class="small">Follow</a>
{% else %}
<a href="{{ url_for('unfollow', username=user.username) }}" class="small">Unfollow</a>
{% endif %}
{% endif %}
{% endif %}
</div>
</div>
{{ super() }}
{% endblock %}
在您完成的列表函数中,您 select 与用户 或 (|) 对应的所有列表已完成。这两个条件都应满足 select 完成的列表,因此您需要 and (&)
我正在使用烧瓶。数据库是sqlite3。 简而言之。我可以制作列表,我可以将它们勾选为 "finished",我想在用户配置文件中显示完成列表的数量。
此时显示与"all lists"相同..
这是我的列表模型:
class List(Model):
timestamp = DateTimeField(default=datetime.datetime.now)
user = ForeignKeyField(
rel_model=User,
related_name='list'
)
content = TextField()
finished = BooleanField(default=False)
class Meta:
database = DATABASE
order_by = ('-timestamp',)
这是我在用户模型上的查找完成列表函数:
def finished_lists(self):
return List.select().where(
(List.user == self) |
(List.finished == True)
)
这是个人资料模板:
{% extends "stream.html" %}
{% block content %}
<div class="row">
<div class="">
<h1>{{ user.username }}</h1>
</div>
<div class="">
<div class="">
<h5>Followers</h5>
<p>{{ user.followers().count() }}</p>
</div>
<div class="">
<h5>Following</h5>
<p>{{ user.following().count() }}</p>
</div>
<div class="">
<h5>Lists</h5>
<p>{{ user.list.count() }}</p>
</div>
<div class="">
<h5>Finished</h5>
<p>{{ user.finished_lists().count() }}</p>
</div>
</div>
<div class="grid-25">
{% if current_user.is_authenticated %}
{% if user != current_user %}
{% if not user in current_user.following() %}
<a href="{{ url_for('follow', username=user.username) }}" class="small">Follow</a>
{% else %}
<a href="{{ url_for('unfollow', username=user.username) }}" class="small">Unfollow</a>
{% endif %}
{% endif %}
{% endif %}
</div>
</div>
{{ super() }}
{% endblock %}
在您完成的列表函数中,您 select 与用户 或 (|) 对应的所有列表已完成。这两个条件都应满足 select 完成的列表,因此您需要 and (&)