In [1]:
    
from watson import search as watson
    
In [2]:
    
%%time
search_results = watson.search("claas")
for result in search_results:
    print(result.title, result.url, result.meta)
    
    
In [3]:
    
Post.objects.count()
    
    Out[3]:
In [4]:
    
%%time
for post in watson.filter(Post, "claas"):
    print(post.title, post.pk)
    
    
In [37]:
    
from django.db import models
from django.db.models.functions import TruncYear
from django.db.models.functions import TruncMonth
    
In [19]:
    
#queryset = watson.filter(Post, "claas")
queryset = Post.objects.all()
fc_queryset = (queryset.order_by()
                       .annotate(year=TruncYear("visible_date"))
                       .values("year").annotate(n=models.Count("pk"))
                       .annotate(month=TruncMonth("visible_date")))
                       #.values("month").annotate(n=models.Count("pk")))
year_counts = {}
for row in fc_queryset:
    print(row)
    #year_counts[row['year']] = year_counts.get(
    #    row['year'], 0
    #) + row['n']
    
    
In [22]:
    
#queryset = watson.filter(Post, "claas")
queryset = Post.objects.all()
fc_queryset = (queryset.order_by()
                       .annotate(month=TruncMonth("visible_date"))
                       .values("month").annotate(n=models.Count("pk")))
year_month_counts = {}
for row in fc_queryset:
    print(row)
    year_month_counts[row["month"]] = row["n"]
    #year_counts[row['year']] = year_counts.get(
    #    row['year'], 0
    #) + row['n']
    
    
In [10]:
    
year_counts
    
    Out[10]:
In [15]:
    
year_counts
    
    Out[15]:
In [25]:
    
for ym, count in sorted(year_month_counts.items()):
    print(ym.strftime("%Y-%m"), ":", count)
    
    
In [33]:
    
for blog in Blog.objects.all():
    print(blog.pk, blog.slug)
    
    
In [14]:
    
from cast.views import PostsListView
    
In [13]:
    
from cast.filters import PostFilter
from django_filters.filterset import filterset_factory
    
In [23]:
    
class DummyRequest:
    def __init__(self, get={}):
        self.GET = get
    
In [27]:
    
pl = PostsListView()
pl.kwargs = {"slug": "das_claas_blog"}
pl.request = DummyRequest()
    
In [30]:
    
kwargs = pl.get_filterset_kwargs(PostFilter)
f = PostFilter(**kwargs)
    
In [31]:
    
f.qs
    
    Out[31]:
In [34]:
    
f = PostFilter(blog=Blog.objects.get(pk=2))
    
In [35]:
    
f.qs
    
    Out[35]:
In [38]:
    
queryset = f.qs
fc_queryset = (queryset.order_by()
                       .annotate(month=TruncMonth("visible_date"))
                       .values("month").annotate(n=models.Count("pk")))
year_month_counts = {}
for row in fc_queryset:
    print(row)
    year_month_counts[row["month"]] = row["n"]
    
    
In [39]:
    
for ym, count in sorted(year_month_counts.items()):
    print(ym.strftime("%Y-%m"), ":", count)
    
    
In [40]:
    
from datetime import datetime
    
In [41]:
    
year_month = datetime.strptime("2019-01", "%Y-%m")
    
In [42]:
    
year_month
    
    Out[42]:
In [43]:
    
year_month.year
    
    Out[43]:
In [44]:
    
year_month.month
    
    Out[44]:
In [45]:
    
blog
    
    Out[45]:
In [50]:
    
qs = (Post.objects.filter(blog=blog)
          .filter(visible_date__year=2018, visible_date__month=2))
    
In [51]:
    
qs.count()
    
    Out[51]:
In [52]:
    
for row in qs:
    print(row)
    
    
In [ ]: