Django annotate()错误AttributeError:' CharField'对象没有属性' resolve_expression'

时间:2016-02-09 10:12:59

标签: python django

您好我想将更多字段连接到django,但即使是这个简单的代码:

    Project.objects.annotate(
        companyname=Concat('company__name',Value('ahoj')),output_field=CharField()    
    )

给我一​​个错误:

AttributeError: 'CharField' object has no attribute 'resolve_expression'

回溯:

  File "/root/MUP/djangoenv/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/root/MUP/djangoenv/lib/python3.4/site-packages/django/db/models/query.py", line 908, in annotate
    clone.query.add_annotation(annotation, alias, is_summary=False)
  File "/root/MUP/djangoenv/lib/python3.4/site-packages/django/db/models/sql/query.py", line 986, in add_annotation
    annotation = annotation.resolve_expression(self, allow_joins=True, reuse=None,
AttributeError: 'CharField' object has no attribute 'resolve_expression'

1 个答案:

答案 0 :(得分:23)

你在错误的地方有一个右括号。 output_fieldContcat的参数,而不是annotate的参数。它应该是:

Project.objects.annotate(
    companyname=Concat('company__name', Value('ahoj'), output_field=CharField()),    
)