Django Queryset Batches












1















I have a Django app that runs on SQL Server. SQL Server allows a maximum of 2,100 parameters in a user-defined function. I have a Django view with a queryset that should return 10,000 results when called:



def result_list(request):
results = MyModel.objects.filter(~(Q(importantField='') | Q(importantField='Some Text'))).select_related('field','field2','field3','field4').prefetch_related('anotherField')
return render(request, 'results/result_list.html', {'results':results})


I have a template that attempts to return those 10,000 results



{% for result in results %}
<!-- put results into an HTML table -->
{% endfor %}


Error:



Request Method: GET
Request URL: https://server/results/result_list

Django Version: 1.11.3
Python Version: 3.6.3

Traceback:

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
65. return self.cursor.execute(sql, params)

File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
545. return self.cursor.execute(sql, params)

The above exception (('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')) was the direct cause of the following exception:

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersexception.py" in inner
41. response = get_response(request)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocontribauthdecorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)

File "D:Websitesprojectappviews.py" in result_list
476. return render(request, 'results/result_list.html', {'results':results})

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoshortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader.py" in render_to_string
68. return template.render(context, request)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebackendsdjango.py" in render
66. return self.template.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
207. return self._render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
107. return self.nodelist.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
990. bit = node.render_annotated(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
957. return self.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
177. return compiled_parent._render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
107. return self.nodelist.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
990. bit = node.render_annotated(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
957. return self.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
72. result = block.nodelist.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
990. bit = node.render_annotated(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
957. return self.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
216. return template.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
209. return self._render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
107. return self.nodelist.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
990. bit = node.render_annotated(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
957. return self.render(context)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatedefaulttags.py" in render
173. len_values = len(values)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __len__
232. self._fetch_all()

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
1120. self._prefetch_related_objects()

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _prefetch_related_objects
675. prefetch_related_objects(self._result_cache, *self._prefetch_related_lookups)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_related_objects
1469. obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_one_level
1599. all_related_objects = list(rel_qs)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
250. self._fetch_all()

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
1118. self._result_cache = list(self._iterable_class(self))

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
886. raise original_exception

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
876. cursor.execute(sql, params)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
80. return super(CursorDebugWrapper, self).execute(sql, params)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
65. return self.cursor.execute(sql, params)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbutils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoutilssix.py" in reraise
685. raise value.with_traceback(tb)

File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
65. return self.cursor.execute(sql, params)

File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
545. return self.cursor.execute(sql, params)

Exception Type: Error at /djangoProject/results/result_list
Exception Value: ('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')


How do I change my view and/or template to batch the ORM query to get around the limit on parameters?



I know the iterator exists, but I believe that queries the database individually for each record, rather than batching queries for every 1,000 or 2,000 records.



I can slice the queryset by adding [:1000] to the end, but I'm not sure how to incorporate that into a loop that unions all of the slices together.










share|improve this question





























    1















    I have a Django app that runs on SQL Server. SQL Server allows a maximum of 2,100 parameters in a user-defined function. I have a Django view with a queryset that should return 10,000 results when called:



    def result_list(request):
    results = MyModel.objects.filter(~(Q(importantField='') | Q(importantField='Some Text'))).select_related('field','field2','field3','field4').prefetch_related('anotherField')
    return render(request, 'results/result_list.html', {'results':results})


    I have a template that attempts to return those 10,000 results



    {% for result in results %}
    <!-- put results into an HTML table -->
    {% endfor %}


    Error:



    Request Method: GET
    Request URL: https://server/results/result_list

    Django Version: 1.11.3
    Python Version: 3.6.3

    Traceback:

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
    65. return self.cursor.execute(sql, params)

    File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
    545. return self.cursor.execute(sql, params)

    The above exception (('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')) was the direct cause of the following exception:

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersexception.py" in inner
    41. response = get_response(request)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
    187. response = self.process_exception_by_middleware(e, request)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
    185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocontribauthdecorators.py" in _wrapped_view
    23. return view_func(request, *args, **kwargs)

    File "D:Websitesprojectappviews.py" in result_list
    476. return render(request, 'results/result_list.html', {'results':results})

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoshortcuts.py" in render
    30. content = loader.render_to_string(template_name, context, request, using=using)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader.py" in render_to_string
    68. return template.render(context, request)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebackendsdjango.py" in render
    66. return self.template.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
    207. return self._render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
    107. return self.nodelist.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
    990. bit = node.render_annotated(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
    957. return self.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
    177. return compiled_parent._render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
    107. return self.nodelist.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
    990. bit = node.render_annotated(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
    957. return self.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
    72. result = block.nodelist.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
    990. bit = node.render_annotated(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
    957. return self.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
    216. return template.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
    209. return self._render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
    107. return self.nodelist.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
    990. bit = node.render_annotated(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
    957. return self.render(context)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatedefaulttags.py" in render
    173. len_values = len(values)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __len__
    232. self._fetch_all()

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
    1120. self._prefetch_related_objects()

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _prefetch_related_objects
    675. prefetch_related_objects(self._result_cache, *self._prefetch_related_lookups)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_related_objects
    1469. obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_one_level
    1599. all_related_objects = list(rel_qs)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
    250. self._fetch_all()

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
    1118. self._result_cache = list(self._iterable_class(self))

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
    53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
    886. raise original_exception

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
    876. cursor.execute(sql, params)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
    80. return super(CursorDebugWrapper, self).execute(sql, params)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
    65. return self.cursor.execute(sql, params)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbutils.py" in __exit__
    94. six.reraise(dj_exc_type, dj_exc_value, traceback)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoutilssix.py" in reraise
    685. raise value.with_traceback(tb)

    File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
    65. return self.cursor.execute(sql, params)

    File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
    545. return self.cursor.execute(sql, params)

    Exception Type: Error at /djangoProject/results/result_list
    Exception Value: ('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')


    How do I change my view and/or template to batch the ORM query to get around the limit on parameters?



    I know the iterator exists, but I believe that queries the database individually for each record, rather than batching queries for every 1,000 or 2,000 records.



    I can slice the queryset by adding [:1000] to the end, but I'm not sure how to incorporate that into a loop that unions all of the slices together.










    share|improve this question



























      1












      1








      1








      I have a Django app that runs on SQL Server. SQL Server allows a maximum of 2,100 parameters in a user-defined function. I have a Django view with a queryset that should return 10,000 results when called:



      def result_list(request):
      results = MyModel.objects.filter(~(Q(importantField='') | Q(importantField='Some Text'))).select_related('field','field2','field3','field4').prefetch_related('anotherField')
      return render(request, 'results/result_list.html', {'results':results})


      I have a template that attempts to return those 10,000 results



      {% for result in results %}
      <!-- put results into an HTML table -->
      {% endfor %}


      Error:



      Request Method: GET
      Request URL: https://server/results/result_list

      Django Version: 1.11.3
      Python Version: 3.6.3

      Traceback:

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      65. return self.cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
      545. return self.cursor.execute(sql, params)

      The above exception (('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')) was the direct cause of the following exception:

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersexception.py" in inner
      41. response = get_response(request)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
      187. response = self.process_exception_by_middleware(e, request)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
      185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocontribauthdecorators.py" in _wrapped_view
      23. return view_func(request, *args, **kwargs)

      File "D:Websitesprojectappviews.py" in result_list
      476. return render(request, 'results/result_list.html', {'results':results})

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoshortcuts.py" in render
      30. content = loader.render_to_string(template_name, context, request, using=using)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader.py" in render_to_string
      68. return template.render(context, request)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebackendsdjango.py" in render
      66. return self.template.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      207. return self._render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
      107. return self.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
      177. return compiled_parent._render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
      107. return self.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
      72. result = block.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
      216. return template.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      209. return self._render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
      107. return self.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatedefaulttags.py" in render
      173. len_values = len(values)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __len__
      232. self._fetch_all()

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
      1120. self._prefetch_related_objects()

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _prefetch_related_objects
      675. prefetch_related_objects(self._result_cache, *self._prefetch_related_lookups)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_related_objects
      1469. obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_one_level
      1599. all_related_objects = list(rel_qs)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
      250. self._fetch_all()

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
      1118. self._result_cache = list(self._iterable_class(self))

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
      53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
      886. raise original_exception

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
      876. cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      80. return super(CursorDebugWrapper, self).execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      65. return self.cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbutils.py" in __exit__
      94. six.reraise(dj_exc_type, dj_exc_value, traceback)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoutilssix.py" in reraise
      685. raise value.with_traceback(tb)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      65. return self.cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
      545. return self.cursor.execute(sql, params)

      Exception Type: Error at /djangoProject/results/result_list
      Exception Value: ('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')


      How do I change my view and/or template to batch the ORM query to get around the limit on parameters?



      I know the iterator exists, but I believe that queries the database individually for each record, rather than batching queries for every 1,000 or 2,000 records.



      I can slice the queryset by adding [:1000] to the end, but I'm not sure how to incorporate that into a loop that unions all of the slices together.










      share|improve this question
















      I have a Django app that runs on SQL Server. SQL Server allows a maximum of 2,100 parameters in a user-defined function. I have a Django view with a queryset that should return 10,000 results when called:



      def result_list(request):
      results = MyModel.objects.filter(~(Q(importantField='') | Q(importantField='Some Text'))).select_related('field','field2','field3','field4').prefetch_related('anotherField')
      return render(request, 'results/result_list.html', {'results':results})


      I have a template that attempts to return those 10,000 results



      {% for result in results %}
      <!-- put results into an HTML table -->
      {% endfor %}


      Error:



      Request Method: GET
      Request URL: https://server/results/result_list

      Django Version: 1.11.3
      Python Version: 3.6.3

      Traceback:

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      65. return self.cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
      545. return self.cursor.execute(sql, params)

      The above exception (('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')) was the direct cause of the following exception:

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersexception.py" in inner
      41. response = get_response(request)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
      187. response = self.process_exception_by_middleware(e, request)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocorehandlersbase.py" in _get_response
      185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangocontribauthdecorators.py" in _wrapped_view
      23. return view_func(request, *args, **kwargs)

      File "D:Websitesprojectappviews.py" in result_list
      476. return render(request, 'results/result_list.html', {'results':results})

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoshortcuts.py" in render
      30. content = loader.render_to_string(template_name, context, request, using=using)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader.py" in render_to_string
      68. return template.render(context, request)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebackendsdjango.py" in render
      66. return self.template.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      207. return self._render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
      107. return self.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
      177. return compiled_parent._render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
      107. return self.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
      72. result = block.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplateloader_tags.py" in render
      216. return template.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      209. return self._render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotestutils.py" in instrumented_test_render
      107. return self.nodelist.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render
      990. bit = node.render_annotated(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatebase.py" in render_annotated
      957. return self.render(context)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangotemplatedefaulttags.py" in render
      173. len_values = len(values)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __len__
      232. self._fetch_all()

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
      1120. self._prefetch_related_objects()

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _prefetch_related_objects
      675. prefetch_related_objects(self._result_cache, *self._prefetch_related_lookups)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_related_objects
      1469. obj_list, additional_lookups = prefetch_one_level(obj_list, prefetcher, lookup, level)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in prefetch_one_level
      1599. all_related_objects = list(rel_qs)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
      250. self._fetch_all()

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in _fetch_all
      1118. self._result_cache = list(self._iterable_class(self))

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelsquery.py" in __iter__
      53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
      886. raise original_exception

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
      876. cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      80. return super(CursorDebugWrapper, self).execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      65. return self.cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbutils.py" in __exit__
      94. six.reraise(dj_exc_type, dj_exc_value, traceback)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangoutilssix.py" in reraise
      685. raise value.with_traceback(tb)

      File "D:PythonAnaconda3envsdjangolibsite-packagesdjangodbbackendsutils.py" in execute
      65. return self.cursor.execute(sql, params)

      File "D:PythonAnaconda3envsdjangolibsite-packagessql_serverpyodbcbase.py" in execute
      545. return self.cursor.execute(sql, params)

      Exception Type: Error at /djangoProject/results/result_list
      Exception Value: ('07002', '[07002] [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')


      How do I change my view and/or template to batch the ORM query to get around the limit on parameters?



      I know the iterator exists, but I believe that queries the database individually for each record, rather than batching queries for every 1,000 or 2,000 records.



      I can slice the queryset by adding [:1000] to the end, but I'm not sure how to incorporate that into a loop that unions all of the slices together.







      python sql sql-server django python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 18:53







      OverflowingTheGlass

















      asked Nov 20 '18 at 18:15









      OverflowingTheGlassOverflowingTheGlass

      788523




      788523
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399114%2fdjango-queryset-batches%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399114%2fdjango-queryset-batches%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          Npm cannot find a required file even through it is in the searched directory

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith