I have a model:
class Motocycle(models.Model):
title = models.CharField(max_length=50, blank=True, default='')
engine_displacement = models.IntegerField(default=0)
and I want to:
queryset = Motocycle.objects.annotate(
full_name=Concat(
'title',
Value(' '),
F('engine_displacement'),
Value('')
),
).all()
But got an error: Expression contains mixed types. You must set output_field
:
queryset = Motocycle.objects.annotate(
full_name=Concat(
'title',
Value(' '),
F('engine_displacement'),
Value(''),
),
output_field=CharField(),
).all()
I tryed to set this output_field
, result was: 'CharField' object has no attribute 'resolve_expression'
.
What I'm doing wrong? Thank you.
You must pass output_field
to your db function, in your code you pass it to annotate.
queryset = Motocycle.objects.annotate(
full_name=Concat(
'title',
Value(' '),
F('engine_displacement'),
Value(''),
output_field=CharField(),
),
).all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With