代码之家  ›  专栏  ›  技术社区  ›  User

QuerySet上的range()用于QuerySet上的tqdm

  •  0
  • User  · 技术社区  · 6 年前

    range() 所以我可以得到总循环迭代次数。

    rows = DjangoObjects.objects.all()
    for i in tqdm(range(rows)):
        row = rows[i]
        ...
    

    错误:

    range() integer end argument expected, got QuerySet.
    

    代码尝试2:

    rows = DjangoObjects.objects.all()
    for row in tqdm(rows):
        ...
    

    错误:

    Exception AttributeError: "'tqdm' object has no attribute 'disable'" in <object repr() failed> ignored
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Bober    6 年前

    为什么?tqdm()接受任何iterable,您不必使用range()。如果循环中需要索引,请使用enumerate()。

    编辑: 你读过吗 tqdm documentation

    with tqdm(total=len(rows)) as pbar:
        for row in rows:
            # Do whatever with your query result
            pbar.update(1)