[Django]-How do I get a constant column in a Django ORM?

3👍

When executing your code, I get the following error:

FieldError('Cannot resolve expression type, unknown output_field')

Which means django doesn’t know the type that your annotated field should result in.

To fix this, you need to wrap your annotation in an ExpressionWrapper:

from django.db.models import Value, ExpressionWrapper

Book.objects.annotate(a=ExpressionWrapper(
    Value('a'), 
    output_field=models.CharField(max_length=45),
))

Leave a comment