Expression contains mixed types: SmallIntegerField, BigIntegerField. You must set output_field

Viewed 503

I create a model in django I want to set monthly_wage as monthly_wage=working_days*daily_wage in the annotation

from django.db.models import F


class AnnotationManager(models.Manager):

    def __init__(self, **kwargs):
         super().__init__()
         self.annotations = kwargs

    def get_queryset(self):
         return super().get_queryset().annotate(**self.annotations)




class DetailsList(models.Model):



    month_list=models.ForeignKey('MonthList',on_delete=models.CASCADE,verbose_name='لیست ماهانه  ') 
    worker=models.ForeignKey('Workers',on_delete=models.CASCADE,verbose_name=' نام کارگر')
    working_days=models.SmallIntegerField('تعداد روز کارکرد')
    daily_wage=models.BigIntegerField('دستمزد روزانه')
    advantage=models.BigIntegerField('مزایای ماهانه')

    _monthly_wage=0

    objects = AnnotationManager(

        monthly_wage=F('working_days') * F('daily_wage')


     )

but because working_days is smallinteger and daily_wage is biginteger

this error raise:

Expression contains mixed types: SmallIntegerField, BigIntegerField. You must set output_field.

How can I fix this

1 Answers

You set the output_field, with an ExpressionWrapper [Django-doc] for example:

from django.db.models import BigIntegerField, ExpressionWrapper

class DetailsList(models.Model):
    # …
    objects = AnnotationManager(
        monthly_wage=ExpressionWrapper(
            F('working_days') * F('daily_wage'),
            output_field=BigIntegerField()
        )
    )
Related