-
Notifications
You must be signed in to change notification settings - Fork 11
/
query_examples.py
43 lines (26 loc) · 1.22 KB
/
query_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
### https://docs.djangoproject.com/en/dev/topics/db/queries/
### to start django shell:
#python manage.py shell
### to import your models and data
from roster.models import Student, Course
### select all
students = Student.objects.all()
### select a single, specific student
student = Student.objects.get(pk=1)
course= Course.objects.get(callnumber="J187")
### select any item with a common data point using filter
fall2012Courses = Course.objects.filter(term="Fall 2012")
fall2012Courses = Course.objects.all.filter(term="Fall 2012") #same as above just shorter
###Querys build off of each other
fallCourses = fall2012Cources.filter(term__startswith="Fall")
##using exclude
nonAdvancedFallCources = fallCources.exclude(name__contains="Advanced")
## complex queries OR
easyCourses = Course.objects.filter(name__startswith='Intro') | Course.objects.filter(name__startswith='Intermediate')
## complex queries AND
hardCourses = Course.objects.exclude(name__startswith='Intro') & Course.objects.exclude(name__startswith='Intermediate')
## orderby
fall2012Courses = Course.objects.filter(term="Fall 2012").order_by('name')
## reverse! (order in opposit order)
fall2012Courses.reverse()
fall2012Courses.reverse()[:1] #last item in list