小编pur*_*has的帖子

django - 在查询集上切片/过滤后过滤,其中结果已被限制

难以理解为什么我无法在查询集上切片后进行过滤以及发生了什么.

stuff = stuff.objects.all()
stuff.count()
Run Code Online (Sandbox Code Playgroud)

= 7

如果我然后去

extra_stuff = stuff.filter(stuff_flag=id)
extra_stuff.count()
Run Code Online (Sandbox Code Playgroud)

= 6.一切都很好,我的新查询集在extrastuff没有问题

stuff = stuff.objects.all()[:3]
extra_stuff = stuff.filter(stuff_flag=id)
Run Code Online (Sandbox Code Playgroud)

我收到错误"切片一旦切片就无法过滤查询".

如何在我限制结果数量的查询集中进一步过滤?

python django

6
推荐指数
2
解决办法
3980
查看次数

python args不工作,除非它有一个位置引用

def test_stats(team, *args):

    if not args:
          [do some stuff]
    else:

        team_fixtures = (Fixtures.objects.filter(home_team=team_details.id) | Fixtures.objects.filter(away_team=team_details.id))/
.filter(fixture_datetime__lt=datetime.now()).filter(fixture_datetime__year=args[0])
Run Code Online (Sandbox Code Playgroud)

为了参考起见 - args是:

date_year = datetime.now().year
Run Code Online (Sandbox Code Playgroud)

为了使这个查询工作,我需要引用args为

.filter(fixture_datetime__year=args[0])
Run Code Online (Sandbox Code Playgroud)

因为如果我使用

.filter(fixture_datetime__year=args)
Run Code Online (Sandbox Code Playgroud)

我收到错误:

int()参数必须是字符串,类似字节的对象或数字,而不是'元组'

我知道它认为它是一个元组,即使它只是一个值,但是当我在终端中执行以下操作时

type(date_year)
Run Code Online (Sandbox Code Playgroud)

我上课了.

当它看起来只有一个值返回时,为什么我必须在这里引用位置?

python args

5
推荐指数
1
解决办法
148
查看次数

标签 统计

python ×2

args ×1

django ×1