-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
查询我关注的及屏蔽的节点 #11
Comments
翻译成 Django ORM 对应语句如果尝试手动使用 Django ORM 来构造左外联接,一般会是失败的。因为 Django ORM 提交使用其提供的关联管理。
users = models.ManyToManyField(WepostUser, through='UserNodeStar', through_fields=('node', 'user'))
相关文档引用如下:
最后实现的代码如下: def _query_related_nodes(self, state: UserNodeStarState, sort_type: UserNodeSortType = UserNodeSortType.CREATED,
keyword: str = None):
if sort_type == UserNodeSortType.ACTIVE_DEGREE:
sort = SortField("active_degree", SortDirection.DESC)
elif sort_type == UserNodeSortType.UPDATED:
sort = SortField("updated", SortDirection.DESC)
else:
sort = SortField("usernodestar__created", SortDirection.DESC)
node_set = self.user.node_set
qs = node_set.all().filter(usernodestar__state=state).order_by(sort.ordering)
if keyword:
qs = qs.filter(Q(name__icontains=keyword) | Q(brief__icontains=keyword))
return qs
def query_star_nodes(self, sort_type: UserNodeSortType = UserNodeSortType.CREATED, keyword: str = None):
"""查询已关注的节点"""
return self._query_related_nodes(UserNodeStarState.FOLLOWING, sort_type=sort_type, keyword=keyword)
def query_blocked_nodes(self, keyword: str = None):
"""查询已屏蔽的节点"""
return self._query_related_nodes(UserNodeStarState.BLOCKING, keyword=keyword) 单元测试代码如下: def test_query_nodes(ut1, node_c, node_py, node_js, node_flask, node_vue):
service = UserNodeService(ut1)
nodes = [node_c, node_js, node_py, node_flask]
for node in nodes:
service.star_node(node)
service.block_node(node_vue)
ret_nodes1 = list(service.query_star_nodes())
assert len(ret_nodes1) == len(nodes)
ret_nodes1.reverse()
assert ret_nodes1 == nodes
ret_nodes2 = service.query_star_nodes(keyword="py")
assert list(ret_nodes2) == [node_py]
blocked_nodes = list(service.query_blocked_nodes())
assert blocked_nodes == [node_vue] |
查询功能要求
查询我关注的节点
返回完整的我关注的节点。
排序注意点:
UserNodeStar.created
排序。Node.updated
排序。Node.active_degree
来排序。从排序需求来看,原
Node
节点需要增加active_degree
这个字段。对于查询语句来说,查询语句不能使用单表查询+子查询的方式。
因为排序需要用到关联表的
UserNodeStar.created
字段。从逻辑上来说以
Node
作为主表,而以UserNodeStar
作为右关联表。那么下面是一个以关注时间进行排序的 SQL:
这里使用了
left outer join
是因为如果使用内联接的话,查询集将太大。在关联条件中的
on node.id = link.node_id and link.user_id == 43
这是为了减少 关联集的大小。因为
wepost_posts_usernodestar
是记录所有人对所有节点的关注The text was updated successfully, but these errors were encountered: