Skip to content

Instantly share code, notes, and snippets.

@williln
Last active June 13, 2017 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williln/5736dbf45d32631cfa7b24516b26b380 to your computer and use it in GitHub Desktop.
Save williln/5736dbf45d32631cfa7b24516b26b380 to your computer and use it in GitHub Desktop.

Resources for Pro Django students at Cisco

June 12-13, 2017

Tags and filters

Regular Expressions

Class-Based Views

Testing

Model Managers

Signals

Data normalization and denormalization

Celery

Django REST Framework

Functional Programming

Security

Queries

Answers to Queries prompts

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'))

List Comprehensions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment