Skip to content

Instantly share code, notes, and snippets.

View hossainchisty's full-sized avatar
🌏
Crafting code to build a better digital world.

Hossain Chisty hossainchisty

🌏
Crafting code to build a better digital world.
View GitHub Profile

Cheatsheet for Django QuerySets

Current Django Version: 2.2

Methods that return new QuerySets

Can be chained:

Entry.objects.filter(**kwargs).exclude(**kwargs).order_by(**kwargs)

drf-cheat-sheet

A collection of anything from basics to advanced recommended methods and usages with Django REST Framework for creating browsable and awesome web API's. This could also serve as a quick reference guide.

Here is DRF's official documentation in case you need everything in detail.

Why Django REST Framework?

Summarized from the official docs:

  • Web browsable API
  • Serialization that supports ORM and non-ORM data sources.
@hossainchisty
hossainchisty / app.py
Created January 10, 2022 14:42
Redis Caching in Flask
import os
import json
import redis
import requests
from flask import Flask
from dotenv import load_dotenv
# Loads the .env file🔐
load_dotenv()
@hossainchisty
hossainchisty / get_total_sale_via_month.txt
Last active December 26, 2021 18:26
How to get the total sales of via month in Django
#Note: Let's say we have Sale model total is a field to hold sales amount then we can do like that... we find month via created_at which is DateTimeField.
sales = Sale.objects.all()
months = sales.datetimes('created_at', kind="month")
for month in months:
month_sale = sales.filter(created_at__month=month.month)
month_total = month_sale.aggregate(Sum('total'))['total__sum']
print(f'Month: {month.strftime("%B")} - Total Sale: {month_total}')