Treat NULL as '0' in Django model

后端 未结 2 1748
遥遥无期
遥遥无期 2020-12-15 01:36

I use the following bit of code in my Django app:

pictures = gallery.picture_set.annotate( score=models.Sum( \'picturevote__value\' ) ).order_by( \'-score\'          


        
2条回答
  •  一向
    一向 (楼主)
    2020-12-15 02:14

    From Django 1.8, there is a Coalesce database function. Your query might look like this:

    from django.db.models.functions import Coalesce    
    
    score = self.picturevote_set.aggregate(Coalesce(models.Sum('value'), 0))
    

提交回复
热议问题