gpt4 book ai didi

python - Django 条件注释计数与过滤器(多对多)关注者/下表

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:46 25 4
gpt4 key购买 nike

这对我来说有点令人费解,所以我决定寻求一些帮助。

我拥有的是一个标准用户模型和一个跟踪用户是否相互关注的表,其中包含 from_userto_userfollowing 值(value)观。第一个是我的用户表的外键,第三个是 bool 值。

我想要做的是能够覆盖 django admin 中默认的 get_queryset 方法,以便我可以根据用户拥有的关注者数量进行排序。

我目前遇到的困难是实际注释关注者的数量,这可能是我遗漏和删除的一些小东西,但这是我的代码:

def get_queryset(self, request):
return AuthUser.objects.annotate(
followers_count=Sum(
Case(
When(user_relations__from_user=1, user_relations__to_relation__following=True, then=1),
default=0,
output_field=IntegerField()
)
))

错误的部分 - 我一直坚持使用 user_relations__from_user=1 ,它应该是当前正在评估的用户的 ID。

我需要在所有条目的聚合中执行的操作是我已经在用户模型内的单个案例场景中执行的操作:

@property
def num_followers(self):
return UserRelations.objects.filter(from_user=self, following=True).count()

看起来很简单,但是注释时如何获取from_user=self

编辑:尝试使用 F user_relations__from_user_id__pk=F('id')

时添加堆栈跟踪

附加说明 - 我的 from_userto_user fk 字段实际上名为 from_user_idto_user_id - 希望如此不会导致额外的困惑。对此感到抱歉。

Internal Server Error: /admin/pkm_user/authuser/
Traceback (most recent call last):
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 616, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/utils/decorators.py", line 110, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 233, in inner
return view(request, *args, **kwargs)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/utils/decorators.py", line 34, in _wrapper
return bound_func(*args, **kwargs)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/utils/decorators.py", line 110, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/utils/decorators.py", line 30, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1548, in changelist_view
self.list_max_show_all, self.list_editable, self)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/contrib/admin/views/main.py", line 47, in __init__
self.root_queryset = model_admin.get_queryset(request)
File "/home/bastor/Work/pokemall-api/django/pkm_user/admin.py", line 65, in get_queryset
output_field=IntegerField()
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/query.py", line 794, in annotate
obj.query.add_annotation(annotation, alias, is_summary=False)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 977, in add_annotation
summarize=is_summary)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/aggregates.py", line 20, in resolve_expression
c = super(Aggregate, self).resolve_expression(query, allow_joins, reuse, summarize)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/expressions.py", line 491, in resolve_expression
c.source_expressions[pos] = arg.resolve_expression(query, allow_joins, reuse, summarize, for_save)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/expressions.py", line 779, in resolve_expression
c.cases[pos] = case.resolve_expression(query, allow_joins, reuse, summarize, for_save)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/expressions.py", line 713, in resolve_expression
c.condition = c.condition.resolve_expression(query, allow_joins, reuse, summarize, False)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/query_utils.py", line 91, in resolve_expression
clause, joins = query._add_q(self, reuse, allow_joins=allow_joins, split_subq=False)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1332, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1194, in build_filter
lookups, value)
File "/home/bastor/Work/pokemall-api/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1709, in get_lookup_constraint
raise exceptions.FieldError('Relation fields do not support nested lookups')
FieldError: Relation fields do not support nested lookups

最佳答案

我认为您正在寻找F expressions :

from django.db.models import F

def get_queryset(self, request):
return AuthUser.objects.annotate(
followers_count=Sum(
Case(
When(user_relations__to_relation__from_user__id=F('id'),
user_relations__to_relation__following=True, then=1),
default=0,
output_field=IntegerField()
)
))

关于python - Django 条件注释计数与过滤器(多对多)关注者/下表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37460639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com