how do i obtain a constant column in a django orm?

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),
))

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top