June 12-13, 2017
- Readable Regular Expressions - 3-hour tutorial
- Classy Class-Based Views
- Views can be classy! - Video presentation
- Docs
- Tutorial
- Example by Kenny Yarboro with Slides - And an accompanying video
- Functional Programming in Python - Free ebook
Retrieve all Question objects
Question.objects.all()
Retrieve all Question objects that contain “favorite”
Question.objects.filter(question_text__contains="favorite")
Retrieve all Question objects created in this month
Question.objects.filter(pub_date__month=6)
Retrieve all Question objects except ones that contain “Which”
Question.objects.all().exclude(question_text__contain="Which")
List all questions created yesterday
import datetime
from django.utils import timezone
time = timezone.now() + datetime.timedelta(days=-1)
Question.objects.filter(pub_date__gte=time)
List all questions you created:
Question.objects.filter(owner__username="lacey")
How many questions did you create?
Question.objects.filter(owner__username="lacey").count()
Total question:
Question.objects.all().count()
Max date a question was created
from django.db.models import Max
Question.objects.all().aggregate(Max('pub_date'))
Create an annotation of the number of choices a question has
questions = Question.objects.annotate(num_choices=Count('choice'))
questions[0].num_choices
What is the max number of choices a question has?
questions.aggregate(Max('num_choices'))