【问题标题】:AttributeError: 'Query' object has no attribute 'aggregate_select'AttributeError:“查询”对象没有属性“聚合选择”
【发布时间】:2017-06-02 21:08:52
【问题描述】:

我正在用 Django 编写一个小脚本。它基于由 cron 执行的 Command 类。我正在使用带有 MS-SQL 的 pyodbc。

class Command(BaseCommand):
    help = 'Sends email notifications'

    def handle(self, *args, **options):
        all_open_port = MyEmailModel.objects.using('emaildb').filter(u_sentinstant__gte=datetime.date.today())

        for p in all_open_port:
            self.stdout.write(p.u_sentinstant)

        self.stdout.write(self.style.SUCCESS('Successfully finished.'))

在评估 QuerySet 时,我总是收到此错误:

AttributeError: 'Query' 对象没有属性 'aggregate_select'

我无法解决这个问题,因为代码与文档相同...

追溯:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Users\user\Desktop\CarrierExchange\cusnotifier\management\commands\sendnotifications.py", line 12, in handle
    for p in all_open_port:
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 256, in __iter__
    self._fetch_all()
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django\db\models\sql\compiler.py", line 824, in execute_sql
    sql, params = self.as_sql()
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django_pyodbc\compiler.py", line 204, in as_sql
    self._fix_aggregates()
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\django_pyodbc\compiler.py", line 180, in _fix_aggregates
    for alias, aggregate in self.query.aggregate_select.items():
AttributeError: 'Query' object has no attribute 'aggregate_select'

【问题讨论】:

  • 我相信您需要在class ...之后缩进所有内容
  • 发布完整的回溯。您发布的代码不包含aggregate_select,因此该错误是在您的应用程序的其他地方触发的。
  • 嗨,我发布了我的 Traceback。代码在课后有缩进,但是当我将它粘贴到 stackoverflow 时它被删除了。

标签: python django pyodbc


【解决方案1】:

似乎是最新的 pyodbc / django-pyodbc 中的一个错误: https://github.com/lionheart/django-pyodbc/pull/146/files

在“lib\site-packages\django_pyodbc\compiler.py”中将“aggregate_select”替换为“annotation_select”可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    相关资源
    最近更新 更多