Current Django Version: 2.2
Methods that return new QuerySets
Can be chained:
Entry.objects.filter(**kwargs).exclude(**kwargs).order_by(**kwargs)
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.
Summarized from the official docs:
import os | |
import json | |
import redis | |
import requests | |
from flask import Flask | |
from dotenv import load_dotenv | |
# Loads the .env file🔐 | |
load_dotenv() |
#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}') |