Django annotate groupings by month

后端 未结 4 1088
猫巷女王i
猫巷女王i 2020-12-08 06:13

I have a very basic model:

class Link(models.Model):
    title = models.CharField(max_length=250, null=False)
    user = models.ForeignKey(User)
    url = mo         


        
4条回答
  •  抹茶落季
    2020-12-08 06:27

    I've read that .extra() will be deprecated in the future. They are suggesting to instead use Func objects. And there is one for extracting a month without using a painful Case statement.

    from django.db.models.functions import ExtractMonth
    Link.objects.all().annotate(pub_date_month=ExtractMonth('pub_date'))
    

提交回复
热议问题