Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django annotate() error AttributeError: 'CharField' object has no attribute 'resolve_expression'

Tags:

python

django

Hello I want to concatenate more fields into django, but even this simple code:

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

Gives me an error:

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

Traceback:

  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'
like image 233
Adam Avatar asked Feb 09 '16 10:02

Adam


1 Answers

You have a closing parenthesis in the wrong place. The output_field is an argument for Concat, not for annotate. It should be:

Project.objects.annotate(
    companyname=Concat('company__name', Value('ahoj'), output_field=CharField()),    
)
like image 147
Alasdair Avatar answered Nov 14 '22 15:11

Alasdair