首页 > 解决方案 > “命令不同步;您现在无法运行此命令”在 django 模型上运行分页器时

问题描述

当我回填一些模型条目时,查询集很大。我在模型上使用了分页器。它运行一次时工作正常。如果我再次执行脚本,它会给我“命令不同步;您现在无法运行此命令”。我知道当有共享连接/连接没有关闭/提交时会发生这种情况。

 for i in paginator.page_range:
    current_page = paginator.page(i)
    for txn in current_page:
       pass

错误堆栈跟踪:

     42     paginator = Paginator(transactions, 100)
     43     #for transaction in transactions.iterator(): #it was still loading everything in memory
---> 44     for i in paginator.page_range:
     45         current_page = paginator.page(i)
     46         for txn in current_page:

/usr/local/lib/python2.7/site-packages/django/core/paginator.pyc in page_range(self)
    100         a template for loop.
    101         """
--> 102         return six.moves.range(1, self.num_pages + 1)
    103 
    104     def _check_object_list_is_ordered(self):

/usr/local/lib/python2.7/site-packages/django/utils/functional.pyc in __get__(self, instance, cls)
     33         if instance is None:
     34             return self
---> 35         res = instance.__dict__[self.name] = self.func(instance)
     36         return res
     37 

/usr/local/lib/python2.7/site-packages/django/core/paginator.pyc in num_pages(self)
     89         Returns the total number of pages.
     90         """
---> 91         if self.count == 0 and not self.allow_empty_first_page:
     92             return 0
     93         hits = max(1, self.count - self.orphans)

/usr/local/lib/python2.7/site-packages/django/utils/functional.pyc in __get__(self, instance, cls)
     33         if instance is None:
     34             return self
---> 35         res = instance.__dict__[self.name] = self.func(instance)
     36         return res
     37 

/usr/local/lib/python2.7/site-packages/django/core/paginator.pyc in count(self)
     77         """
     78         try:
---> 79             return self.object_list.count()
     80         except (AttributeError, TypeError):
     81             # AttributeError if object_list has no count() method.

/usr/local/lib/python2.7/site-packages/django/db/models/query.pyc in count(self)
    362             return len(self._result_cache)
    363 
--> 364         return self.query.get_count(using=self.db)
    365 
    366     def get(self, *args, **kwargs):

/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in get_count(self, using)
    497         obj = self.clone()
    498         obj.add_annotation(Count('*'), alias='__count', is_summary=True)
--> 499         number = obj.get_aggregation(using, ['__count'])['__count']
    500         if number is None:
    501             number = 0

/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in get_aggregation(self, using, added_aggregate_names)
    478         outer_query.select_related = False
    479         compiler = outer_query.get_compiler(using)
--> 480         result = compiler.execute_sql(SINGLE)
    481         if result is None:
    482             result = [None for q in outer_query.annotation_select.items()]

/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type, chunked_fetch)
    897                 # silencing when dropping Python 2 compatibility.
    898                 pass
--> 899             raise original_exception
    900 
    901         if result_type == CURSOR:

ProgrammingError: (2014, "Commands out of sync; you can't run this command now")

标签: pythondjangopaginator

解决方案


推荐阅读