Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django annotate

from datetime import timedelta
from django.utils import timezone
from django.db.models import Count, Q # need import

Article.objects.annotate(
    numviews=Count(
        'readership__reader__id', 
        filter=Q(readership__what_time__gt=timezone.now() - timedelta(minutes=30)), 
        distinct=True
    )
)


from django.db.models import Count, Case, When, IntegerField
Article.objects.annotate(
    numviews=Count(Case(
        When(readership__what_time__lt=treshold, then=1),
        output_field=IntegerField(),
    ))
)
Comment

PREVIOUS NEXT
Code Example
Python :: how to reverse list python 
Python :: length of dictionary in python 
Python :: django raw without sql injection 
Python :: pandas read csv with lists 
Python :: how to write a function in python 
Python :: production mode flask 
Python :: astype float across columns pandas 
Python :: dictionary input from user in python3 
Python :: while loop with if else 
Python :: python dynamic variable name 
Python :: python string lenght 
Python :: hmac sha256 python 
Python :: how to convert string into list in python 
Python :: check boolean python 
Python :: find index of value in list python 
Python :: python isdigit 
Python :: How to Loop Through Tuples using for loop in python 
Python :: python regex split 
Python :: empty list check in python 
Python :: permutation and combination program in python 
Python :: python order number list 
Python :: smtp python 
Python :: polymorphism in python 
Python :: elif python 
Python :: upgrade python version windows 
Python :: list all files in a folder 
Python :: List Get a Element 
Python :: how to import somthing from another directory in pyhon 
Python :: python floor function 
Python :: cross validation sklearn 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =