139
Introduction to Django Jacob Kaplan-Moss Strange Loop 2011 http://lanyrd.com/sfypm

Introduction To Django (Strange Loop 2011)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction To Django (Strange Loop 2011)

Introduction to Django

Jacob Kaplan-Moss

Strange Loop 2011http://lanyrd.com/sfypm

Page 2: Introduction To Django (Strange Loop 2011)

Django Training

Part 1: Introduction to Django.

Page 3: Introduction To Django (Strange Loop 2011)

“”

Django is a high-level Python web framework that encourages rapid

development and clean, pragmatic design.

2

Page 4: Introduction To Django (Strange Loop 2011)

“”

Django is a high-level Python web framework that encourages rapid

development and clean, pragmatic design.

3

Page 5: Introduction To Django (Strange Loop 2011)

“”

Django is a high-level Python web framework that encourages rapid

development and clean, pragmatic design.

4

Page 6: Introduction To Django (Strange Loop 2011)

“”

Django is a high-level Python web framework that encourages rapid

development and clean, pragmatic design.

5

Page 8: Introduction To Django (Strange Loop 2011)

Which Django version should I use?

7

Page 9: Introduction To Django (Strange Loop 2011)

Installing Django

8

•Download and run http://bit.ly/dsetup (http://python-­‐distribute.org/distribute_setup.py)

•easy_install  Django

•Later: learn about pip and virtualenvhttp://pip.rtfd.org/http://virtualenv.rtfd.org/

Page 10: Introduction To Django (Strange Loop 2011)

“Projects”

9

Page 11: Introduction To Django (Strange Loop 2011)

$  django-­‐admin.py  startproject  yabl

10

Page 12: Introduction To Django (Strange Loop 2011)

yabl/__init__.pymanage.pysettings.pyurls.py

11

Page 13: Introduction To Django (Strange Loop 2011)

$  python  manage.py  runserverValidating  models...0  errors  found.

Django  version  1.1  beta  1  SVN-­‐10844,  using  settings  'yabl.settings'Development  server  is  running  at  http://127.0.0.1:8000/Quit  the  server  with  CONTROL-­‐C.

12

Page 14: Introduction To Django (Strange Loop 2011)

13

Page 15: Introduction To Django (Strange Loop 2011)

•DATABASE_ENGINE

•DATABASE_NAME

•DATABASE_USER

•DATABASE_PASSWORD

•DATABASE_HOST

Project settings

14

Page 16: Introduction To Django (Strange Loop 2011)

$  python  manage.py  syncdbCreating  table  auth_permissionCreating  table  auth_groupCreating  table  auth_userCreating  table  auth_messageCreating  table  django_content_typeCreating  table  django_sessionCreating  table  django_site

You  just  installed  Django's  auth  system,  which  means  you  don't  have  any  superusers  defined.Would  you  like  to  create  one  now?  (yes/no):  yesUsername  (Leave  blank  to  use  'jacob'):  jacobE-­‐mail  address:  [email protected]:  Password  (again):  Superuser  created  successfully.Installing  index  for  auth.Permission  modelInstalling  index  for  auth.Message  model

15

Page 17: Introduction To Django (Strange Loop 2011)

http://django.me/about-settingshttp://django.me/settings

http://django.me/manage.py

Documentation

16

Page 18: Introduction To Django (Strange Loop 2011)

Exercise:“it worked!”

17

Page 19: Introduction To Django (Strange Loop 2011)

Django Training

Part 2: Apps, models, and the admin

Page 20: Introduction To Django (Strange Loop 2011)

“Apps”

2

Page 21: Introduction To Django (Strange Loop 2011)

“Models”

3

Page 22: Introduction To Django (Strange Loop 2011)

What’s a model?

4

Page 23: Introduction To Django (Strange Loop 2011)

MVC?(Model-View-Controller)

5

Page 24: Introduction To Django (Strange Loop 2011)

CREATE  TABLE  "entries_entry"  (        "id"  integer  NOT  NULL  PRIMARY  KEY,        "author_id"  integer  NOT  NULL,        "pub_date"  datetime  NOT  NULL,        "headline"  varchar(200)  NOT  NULL,        "slug"  varchar(50)  NOT  NULL  UNIQUE,        "summary"  text  NOT  NULL,        "body"  text  NOT  NULL)

6

Page 25: Introduction To Django (Strange Loop 2011)

•SQL is tough

•SQL knows no version control

•DRY

•Python is fun!

Scary Quirky Language

7

Page 26: Introduction To Django (Strange Loop 2011)

import  datetimefrom  django.db  import  modelsfrom  yabl.authors.models  import  Author

class  Entry(models.Model):        author              =  models.ForeignKey(Author,  related_name='entries')        pub_date          =  models.DateTimeField(default=datetime.datetime.now)        headline          =  models.CharField(max_length=200)        slug                  =  models.SlugField(unique=True)        summary            =  models.TextField()        body                  =  models.TextField()

8

Page 27: Introduction To Django (Strange Loop 2011)

Defining Models

9

Page 28: Introduction To Django (Strange Loop 2011)

$  python  manage.py  startapp  authors

10

Page 29: Introduction To Django (Strange Loop 2011)

authors/__init__.pymodels.pytests.pyviews.py

11

Page 30: Introduction To Django (Strange Loop 2011)

INSTALLED_APPS  =  (        "django.contrib.auth",        "django.contrib.contenttypes",        "django.contrib.sessions",          "django.contrib.sites",        "yabl.authors",)

12

Page 31: Introduction To Django (Strange Loop 2011)

from  django.db  import  models

class  Author(models.Model):        first_name    =  models.CharField(max_length=200)        last_name      =  models.CharField(max_length=200)        bio                  =  models.TextField(blank=True)

13

Page 32: Introduction To Django (Strange Loop 2011)

$  python  manage.py  validate0  errors  found.

14

Page 33: Introduction To Django (Strange Loop 2011)

$  python  manage.py  sqlall  authorsBEGIN;CREATE  TABLE  "authors_author"  (        "id"  integer  NOT  NULL  PRIMARY  KEY,        "first_name"  varchar(200)  NOT  NULL,        "last_name"  varchar(200)  NOT  NULL,        "bio"  text  NOT  NULL);COMMIT;

15

Page 34: Introduction To Django (Strange Loop 2011)

$  python  manage.py  syncdbCreating  table  authors_authorInstalling  index  for  authors.Author  model

16

Page 35: Introduction To Django (Strange Loop 2011)

$  python  manage.py  shell

[1]  >>>  from  yabl.authors.models  import  Author

[2]  >>>  a  =  Author(first_name="John",  last_name="Barth")

[3]  >>>  a.save()

17

Page 36: Introduction To Django (Strange Loop 2011)

[4]  >>>  Author.objects.all()[4]      :  [<Author:  Author  object>]

[5]  >>>  Author.objects.create(first_name='Miguel',  last_name='de  Cervantes')[5]      :  <Author:  Author  object>

[6]  >>>  Author.objects.all()[6]      :  [<Author:  Author  object>,  <Author:  Author  object>]

[7]  >>>  al  =  Author.objects.filter(first_name='John')

[8]  >>>  al[0].last_name[8]      :  u'Barth'

[9]  >>>  Author.objects.get(last_name__startswith='de').first_name[9]      :  u'Miguel'

18

Page 37: Introduction To Django (Strange Loop 2011)

Model metadata

19

Page 38: Introduction To Django (Strange Loop 2011)

class  Author(models.Model):        first_name    =  models.CharField(max_length=200)        last_name      =  models.CharField(max_length=200)        bio                  =  models.TextField(blank=True)                def  __unicode__(self):                return  '%s  %s'  %  (self.first_name,  self.last_name)

20

Page 39: Introduction To Django (Strange Loop 2011)

class  Author(models.Model):        …

       class  Meta:                verbose_name_plural  =  'authors'                ordering  =  ['last_name',  'first_name']

21

Page 40: Introduction To Django (Strange Loop 2011)

[1]  >>>  from  yabl.authors.models  import  Author

[2]  >>>  Author.objects.all()[2]      :  [<Author:  John  Barth>,  <Author:  Miguel  de  Cervantes>]

[3]  >>>  Author.objects.order_by('-­‐first_name')[3]      :  [<Author:  Miguel  de  Cervantes>,  <Author:  John  Barth>]

22

Page 42: Introduction To Django (Strange Loop 2011)

Exercise:Write some apps and some models:

• Author (authors app)

• first_name (CharField)

• last_name (CharField)

• bio (TextField)

• Entry (entries app)

• author (ForeignKey)

• pub_date (DateTimeField)

• is_published (BooleanField)

• headline (CharField)

• slug (SlugField)

• summary (TextField)

• body (TextField)

24

Page 43: Introduction To Django (Strange Loop 2011)

Django’s admin interface

25

Page 44: Introduction To Django (Strange Loop 2011)

“”

A Web-based interface, limited to trusted site

administrators, that enables the adding, editing and deletion of site content.

— The Django Bookhttp://djangobook.com/en/2.0/chapter06/

26

Page 45: Introduction To Django (Strange Loop 2011)

from  django.contrib  import  adminfrom  yabl.authors.models  import  Author

admin.site.register(Author)

27

Page 46: Introduction To Django (Strange Loop 2011)

INSTALLED_APPS  =  (        'django.contrib.auth',        'django.contrib.contenttypes',        'django.contrib.sessions',        'django.contrib.sites',        'django.contrib.admin',        'yabl.authors',        'yabl.entries',)

28

Page 47: Introduction To Django (Strange Loop 2011)

$  python  manage.py  syncdbCreating  table  django_admin_logInstalling  index  for  admin.LogEntry  model

29

Page 48: Introduction To Django (Strange Loop 2011)

from  django.conf.urls.defaults  import  *

#  Uncomment  the  next  two  lines  to  enable  the  admin:from  django.contrib  import  adminadmin.autodiscover()

urlpatterns  =  patterns('',        #  Example:        #  (r'^yabl/',  include('yabl.foo.urls')),

       #  Uncomment  the  admin/doc  line  below  and  add  'django.contrib.admindocs'          #  to  INSTALLED_APPS  to  enable  admin  documentation:        #  (r'^admin/doc/',  include('django.contrib.admindocs.urls')),

       #  Uncomment  the  next  line  to  enable  the  admin:        (r'^admin/',  include(admin.site.urls)),)

30

Page 49: Introduction To Django (Strange Loop 2011)

31

Page 50: Introduction To Django (Strange Loop 2011)

32

Page 51: Introduction To Django (Strange Loop 2011)

33

Page 52: Introduction To Django (Strange Loop 2011)

34

Page 53: Introduction To Django (Strange Loop 2011)

from  django.contrib  import  adminfrom  yabl.authors.models  import  Author

class  AuthorAdmin(admin.ModelAdmin):        pass

admin.site.register(Author,  AuthorAdmin)

35

Page 54: Introduction To Django (Strange Loop 2011)

Documentationhttp://djangobook.com/en/2.0/chapter06/

http://django.me/admin

36

Page 55: Introduction To Django (Strange Loop 2011)

Exercise:

37

Page 56: Introduction To Django (Strange Loop 2011)

Django Training

Part 3: URLs, views, and templates

Page 57: Introduction To Django (Strange Loop 2011)

Views

2

Page 58: Introduction To Django (Strange Loop 2011)

What’s a view?

3

Page 59: Introduction To Django (Strange Loop 2011)

4

Page 60: Introduction To Django (Strange Loop 2011)

URLs

5

Page 61: Introduction To Django (Strange Loop 2011)

page.php

script.cgi?pageid=144

StoryPage.aspx

6

Page 62: Introduction To Django (Strange Loop 2011)

0,2097,1-1-30-72-407-4752,00.html

7

Page 63: Introduction To Django (Strange Loop 2011)

/authors//authors/jacob//authors/adrian/

8

Page 64: Introduction To Django (Strange Loop 2011)

ROOT_URLCONF  =  "yabl.urls"

9

Page 65: Introduction To Django (Strange Loop 2011)

from  django.conf.urls.defaults  import  *

#  Uncomment  the  next  two  lines  to  enable  the  admin:from  django.contrib  import  adminadmin.autodiscover()

urlpatterns  =  patterns('',        (r'^authors/$',                  'yabl.authors.views.author_list'),        (r'^authors/(\d+)/$',      'yabl.authors.views.author_detail'),        (r'^admin/',  include(admin.site.urls)),)

10

yabl/urls.py

Page 66: Introduction To Django (Strange Loop 2011)

from  django.conf.urls.defaults  import  *

urlpatterns  =  patterns('',        (r'^$',                  'yabl.authors.views.author_list'),        (r'^(\d+)/$',      'yabl.authors.views.author_detail'),)

11

yabl/authors/urls.py

Page 67: Introduction To Django (Strange Loop 2011)

from  django.conf.urls.defaults  import  *

#  Uncomment  the  next  two  lines  to  enable  the  admin:from  django.contrib  import  adminadmin.autodiscover()

urlpatterns  =  patterns('',        (r'^authors/',  include('yabl.authors.urls')),        (r'^admin/',      include(admin.site.urls)),)

12

yabl/urls.py

Page 68: Introduction To Django (Strange Loop 2011)

Regex crash coursea The letter “a”.

a+ One or more “a”s.

b? Zero or one “b”s.

c{1,3} One, two, or three “c”s.

. Any single character.

[abc] Either an “a”, “b”, or “c”.

[A-­‐Z] Any character between “A” and “Z”.

[A-­‐Za-­‐z0-­‐9]? Zero or one letters “A-Z”, “a-z”, or “0-9”.

(\d{3,4}) A group containing three or four digits.

(\w*) A group containing zero or more word characters (letters/digits).

[^/]+ One or more characters until (and not including) a forward slash.

^(joe|bob) A string starting with “joe” or “bob”.

(?P<id>\d+) A group named “id” containing one or more digits.

article/$ A string ending with “article/”

13

Page 69: Introduction To Django (Strange Loop 2011)

•GET  /authors/1/

•ROOT_URLCONF

•yabl.urls

•(r'^authors/',  include('yabl.authors.urls'))

•yabl.authors.urls

•(r'^$',  'author_list')                    (no match)

•(r'^(\d+)/',  'author_detail')      (match!)

•author_detail(request,  '1')

Dissecting a request

14

Page 70: Introduction To Django (Strange Loop 2011)

Documentationhttp://django.me/urls

15

Page 71: Introduction To Django (Strange Loop 2011)

A first view

16

Page 72: Introduction To Django (Strange Loop 2011)

from  django.http  import  HttpResponse

def  author_list(request):        return  HttpResponse("This  is  the  author  list!")

17

yabl/authors/views.py

Page 73: Introduction To Django (Strange Loop 2011)

from  django.http  import  HttpResponsefrom  yabl.authors.models  import  Author

def  author_list(request):        r  =  "<ul>"        for  a  in  Author.objects.all():                r  +=  "<li>%s</li>"  %  a.name        r  +=  "</ul>"        return  HttpResponse(r)

18

yabl/authors/views.py

Page 74: Introduction To Django (Strange Loop 2011)

from  django  import  templatefrom  django.http  import  HttpResponsefrom  yabl.authors.models  import  Author

def  author_list(request):        as  =  Author.objects.all()        tmpl  =  template.loader.get_template("authors/index.html")        context  =  template.Context({"authors":  as})        return  HttpResponse(tmpl.render(context))

19

yabl/authors/views.py

Page 75: Introduction To Django (Strange Loop 2011)

from  django.shortcuts  import  renderfrom  yabl.authors.models  import  Author

def  author_list(request):        context  =  {"authors"  :  Author.objects.all()}        return  render(request,  "authors/index.html",  context)

20

yabl/authors/views.py

Page 76: Introduction To Django (Strange Loop 2011)

from  django.http  import  Http404from  django.shortcuts  import  render_to_responsefrom  yabl.authors.models  import  Author

def  author_detail(request,  author_id):        try:                author  =  Author.objects.get(id=author_id)        except  Author.DoesNotExist:                raise  Http404()

       return  render(request,  "authors/detail.html",  {"author"  :  author})

21

yabl/authors/views.py

Page 77: Introduction To Django (Strange Loop 2011)

from  django.shortcuts  import  render_to_response,  get_object_or_404from  yabl.authors.models  import  Author

def  author_detail(request,  author_id):        author  =  get_object_or_404(Author,  id=author_id)        return  render(request,  "authors/detail.html",  {"author"  :  author})

22

yabl/authors/views.py

Page 78: Introduction To Django (Strange Loop 2011)

Templates

23

Page 79: Introduction To Django (Strange Loop 2011)

What’s a template?

24

Page 80: Introduction To Django (Strange Loop 2011)

<!DOCTYPE  HTML  PUBLIC  "-­‐//W3C//DTD  HTML  4.01//EN"><html  lang="en"><head><title>Authors</title></head><body>

<h1>Authors  ({{  authors|length  }}  total)</h1><ul>

{%  for  a  in  authors  %}<li>

<a  href="{{  a.id  }}/">{{  a.name  }}</a></li>

{%  endfor  %}</ul>

</body></html>

25

Page 81: Introduction To Django (Strange Loop 2011)

Where to templates go?

26

•In an app’s templates directory.

• In directories specified by settings.TEMPLATE_DIRS.

• ...

Page 82: Introduction To Django (Strange Loop 2011)

TEMPLATE_DIRS  =  [        '/path/to/some/templates/',        '/path/to/some/more/other/templates/',]

27

Page 83: Introduction To Django (Strange Loop 2011)

TEMPLATE_DIRS  =  [        '/Users/jacob/Projects/stl-­‐django/yabl/templates/',]

28

Page 84: Introduction To Django (Strange Loop 2011)

<!DOCTYPE  HTML  PUBLIC  "-­‐//W3C//DTD  HTML  4.01//EN"><html  lang="en"><head><title>Authors</title></head><body>

<h1>Authors  ({{  authors|length  }}  total)</h1><ul>

{%  for  a  in  authors  %}<li>

<a  href="{{  a.id  }}/">{{  a.name  }}</a></li>

{%  endfor  %}</ul>

</body></html>

29

Page 85: Introduction To Django (Strange Loop 2011)

•a["name"]

•a.name

•a.name()

The magic dot

30

Page 86: Introduction To Django (Strange Loop 2011)

<!DOCTYPE  HTML  PUBLIC  "-­‐//W3C//DTD  HTML  4.01//EN"><html  lang="en"><head><title>Authors</title></head><body>

<h1>Authors  ({{  authors|length  }}  total)</h1><ul>

{%  for  a  in  authors  %}<li>

<a  href="{{  a.id  }}/">{{  a.name  }}</a></li>

{%  endfor  %}</ul>

</body></html>

31

Page 87: Introduction To Django (Strange Loop 2011)

{{  text|escape|linkbreaks  }}

32

Page 88: Introduction To Django (Strange Loop 2011)

{{  text|truncatewords:"30"  }}

33

Page 89: Introduction To Django (Strange Loop 2011)

<!DOCTYPE  HTML  PUBLIC  "-­‐//W3C//DTD  HTML  4.01//EN"><html  lang="en"><head><title>Authors</title></head><body>

<h1>Authors  ({{  authors|length  }}  total)</h1><ul>

{%  for  a  in  authors  %}<li>

<a  href="{{  a.id  }}/">{{  a.name  }}</a></li>

{%  endfor  %}</ul>

</body></html>

34

Page 90: Introduction To Django (Strange Loop 2011)

Template inheritance

35

Page 91: Introduction To Django (Strange Loop 2011)

<!DOCTYPE  HTML  PUBLIC  "-­‐//W3C//DTD  HTML  4.01//EN"><html  lang="en"><head>

<title>{%  block  title  %}YABL{%  endblock  %}

</title></head><body>

<div  id="content">{%  block  content  %}{%  endblock  %}

</div><div  id="footer">

{%  block  footer  %}Copyright  blah..{%  endblock  %}</div>

</body></html>

36

Page 92: Introduction To Django (Strange Loop 2011)

<!DOCTYPE  HTML  PUBLIC  "-­‐//W3C//DTD  HTML  4.01//EN"><html  lang="en"><head>

<title>{%  block  title  %}YABL{%  endblock  %}

</title></head><body>

<div  id="content">{%  block  content  %}{%  endblock  %}

</div><div  id="footer">

{%  block  footer  %}Copyright  blah..{%  endblock  %}</div>

</body></html>

37

Page 93: Introduction To Django (Strange Loop 2011)

{%  extends  "base.html"  %}

{%  block  title  %}Authors  |  {{  block.super  }}

{%  endblock  %}

{%  block  content  %}<h1>Authors  ({{  authors|length  }}  total)</h1><ul>

{%  for  a  in  authors  %}<li>

<a  href="{{  a.id  }}/">{{  a.name  }}</a></li>

{%  endfor  %}</ul>

{%  endblock  %}

38

Page 94: Introduction To Django (Strange Loop 2011)

section_index.html

!"#$%&$'()#*+,)$-.$'$/0123&45*#"6

!"#+5718#&0&5$#"6

##!!#)$1&07'2&0&5$#66

!"#$'(+5718#"6

!"#+5718#17'&$'&#"6

##93:;!!#)$1&07'2&0&5$#669<3:;

##!"#=7/#)&7/>#0'#)&7/>-50)&#"6

####93?;!!#)&7/>23$,(50'$#669<3?;

####9@;!!#)&7/>2&$,)$#66

##!"#$'(=7/#"6

!"#$'(+5718#"6

base_generic.html

!"#$%&$'()#*+,)$23&45*#"6

!"#+5718#&0&5$#"6

##ABC7/5(2174

!"#$'(+5718#"6

!"#+5718#/,05#"6

##9D5;

####950;E74$9<50;

####950;A71,5#'$F)9<50;

####222

##9<D5;

!"#$'(+5718#"6

base.html

93&45;

93$,(;

##9&0&5$;

####!"#+5718#&0&5$#"6!"#$'(+5718#"6

##9<&0&5$;

9<3$,(;

9+7(>;

##9(0G#0(H*/,05*;

####!"#+5718#/,05#"6!"#$'(+5718#"6

##9<(0G;

##9(0G#0(H*17'&$'&*;

####!"#+5718#17'&$'&#"6!"#$'(+5718#"6

##9<(0G;

9<+7(>;

9<3&45;

! "

#

$

39

Page 95: Introduction To Django (Strange Loop 2011)

Why?

40

Page 96: Introduction To Django (Strange Loop 2011)

Inheritance tips

•{% extends %} must be the first thing in your template.

•More {% block %}s are better.

• If you’re duplicating content, you’re missing a block.

•{{ block.super }}

41

Page 98: Introduction To Django (Strange Loop 2011)

Exercise:/authors/

/authors/{id}/

/entries//entries/{slug}/

43

Page 99: Introduction To Django (Strange Loop 2011)

Django training

BONUS: Models and queries

Page 100: Introduction To Django (Strange Loop 2011)

Terminology

2

Page 101: Introduction To Django (Strange Loop 2011)

Modelsa.k.a. “DDL”

3

Page 102: Introduction To Django (Strange Loop 2011)

Managersa.k.a. “table”

4

Page 103: Introduction To Django (Strange Loop 2011)

QuerySetsa.k.a. “selection”

5

Page 104: Introduction To Django (Strange Loop 2011)

Model instancesa.k.a. “row”

6

Page 105: Introduction To Django (Strange Loop 2011)

!!!"!"#$#%&'()*+,-./'"*012'.(31"456,21"7.8$9(6.:

!!!"!"

#$%&'()*"+,&"-.'/0"123!4"$%&'()*"523"-.'/0"6,&!7

!!!".#$#!";<=

!!!".

$%&'()*"+,&"-.'/0"123!

QuerySetManagerModel Instance

7

Page 106: Introduction To Django (Strange Loop 2011)

Models

8

Page 107: Introduction To Django (Strange Loop 2011)

Instance methods

class  Entry(models.Model):        …

       def  is_by_jacob(self):                return  "jacob"  in  self.author.name.lower()

[1]  >>>  e  =  Entry.objects.get(pk=1)

[2]  >>>  e.is_by_jacob()[2]      :  False

9

Page 108: Introduction To Django (Strange Loop 2011)

“Special” instance methods

10

Page 109: Introduction To Django (Strange Loop 2011)

__unicode__

class  Entry(models.Model):        …

       def  __unicode__(self):                return  self.headline

[1]  >>>  Entry.objects.all()[1]      :  [<Entry:  Man  bites  dog>,  <Entry:  Dog  bites  man>]

11

Page 110: Introduction To Django (Strange Loop 2011)

save

class  Entry(models.Model):        …

       def  save(self,  **kwargs):                self.word_count  =  count_words(self.body)                super(Entry,  self).save(**kwargs)

12

Page 111: Introduction To Django (Strange Loop 2011)

save

class  Entry(models.Model):        …

       def  save(self,  **kwargs):                self.word_count  =  count_words(self.body)                super(Entry,  self).save(**kwargs)

Don’t forget this part!

13

Page 112: Introduction To Django (Strange Loop 2011)

delete

class  Author(models.Model):        …

       def  delete(self):                nobody  =  Author.objects.get(first_name='<NOBODY>')                self.entries.update(author=nobody)                super(Author,  self).delete()

14

Page 113: Introduction To Django (Strange Loop 2011)

Managers

15

Page 114: Introduction To Django (Strange Loop 2011)

Default manager

class  Entry(models.Model):        …

       objects  =  models.Manager()

[1]  >>>  from  yabl.entries.models  import  Entry

[2]  >>>  Entry.objects[2]      :  <django.db.models.manager.Manager  object  at  0x7eca70>

16

Page 115: Introduction To Django (Strange Loop 2011)

Custom managers

class  EntryManager(models.Manager):        def  future(self):                …        def  past(self):                …

class  Entry(models.Model):        …

       objects  =  EntryManager()

17

Page 116: Introduction To Django (Strange Loop 2011)

[1]  >>>  from  yabl.entries.models  import  Entry

[2]  >>>  Entry.objects.future()[2]      :  [<Entry:  Hi>]

[3]  >>>  Entry.objects.past()[3]      :  [<Entry:  Man  bites  dog>,  <Entry:  Dog  bites  man>]

18

Page 117: Introduction To Django (Strange Loop 2011)

Documentationhttp://django.me/managers

19

Page 118: Introduction To Django (Strange Loop 2011)

QuerySets

20

Page 119: Introduction To Django (Strange Loop 2011)

[1]  >>>  Author.objects.filter(first_name='Jacob')[1]      :  [<Author:  Jacob  Kaplan-­‐Moss>]

[2]  >>>  Author.objects.filter(last_name__contains='s')[2]      :  [<Author:  Miguel  de  Cervantes>,  <Author:  Jacob  Kaplan-­‐Moss>]

[3]  >>>  Author.objects.filter(last_name__contains='s',  first_name='Miguel')[3]      :  [<Author:  Miguel  de  Cervantes>]

[4]  >>>  Author.objects.filter(last_name__contains='s').filter(first_name='Miguel')[4]      :  [<Author:  Miguel  de  Cervantes>]

Filters

21

Page 120: Introduction To Django (Strange Loop 2011)

Field lookupsexact,  iexact name__exact='Joe'

contains,  icontains name__icontains='s'

startswith,  endswith,  istartswith,  iendswith

name__endswith='nd'

inname__in=('Joe',  'Jane')

author__in=Author.objects.filter(…)

gt,  gte,  lt,  lte cost__gt=100

rangecost__range=(100,  500)

date__range=(now,  tomrrow)

year,  month,  day,  week_daydate__year=2009

date__month=7

isnull author__isnull=True

regex,  iregex name__regex='^J.*b$'

22

Page 121: Introduction To Django (Strange Loop 2011)

Following relationships

[1]  >>>  Entry.objects.filter(author__first_name__startswith='J')[1]      :  [<Entry:  Hi>]

[2]  >>>  Author.objects.filter(entries__headline='Hi')[2]      :  [<Author:  Jacob  Kaplan-­‐Moss>]

Where’d that come from?

23

Page 122: Introduction To Django (Strange Loop 2011)

related_name

class  Entry(models.Model):        author  =  models.ForeignKey(Author,  related_name='entries')

24

Page 123: Introduction To Django (Strange Loop 2011)

select_related()

[1]  >>>  e  =  Entry.objects.get(pk=1)

[2]  >>>  e.author[2]      :  <Author:  Jacob  Kaplan-­‐Moss>

Oops,  that  did  a  second,  needless  query.

[3]  >>>  e  =  Entry.objects.select_related().get(pk=1)

[4]  >>>  e.author[5]      :  <Author:  Jacob  Kaplan-­‐Moss>

No  second  query  needed  for  e.author

25

Page 124: Introduction To Django (Strange Loop 2011)

Limiting select_related()

[1]  >>>  Entry.objects.select_related('author',  'category')

[2]  >>>  Entry.objects.select_related(depth=2)

26

Page 125: Introduction To Django (Strange Loop 2011)

QuerySet details

27

Page 126: Introduction To Django (Strange Loop 2011)

QuerySets are chainable

[1]  >>>  Entry.objects.filter(    ....:          headline__contains='bites',    ....:  ).exclude(    ....:          pub_date__year=2008    ....:  ).filter(    ....:          pub_date__month=9    ....:  )

28

Page 127: Introduction To Django (Strange Loop 2011)

QuerySets are unique

[1]  >>>  qs1  =  Entry.objects.filter(headline__icontains='dog')

[2]  >>>  qs2  =  qs1.exclude(pub_date__year=2008)

[3]  >>>  qs3  =  qs1.filter(pub_date__year=2008)

29

Page 128: Introduction To Django (Strange Loop 2011)

QuerySets are lazy

[1]  >>>  qs  =  Entry.objects.filter(headline__icontains='dog')

[2]  >>>  qs  =  qs.exclude(pub_date__year=2008)

[3]  >>>  qs  =  qs.filter(author__first_name='Jacob')

[4]  >>>  qs[4]      :  [<Entry:  Man  bites  dog>,  <Entry:  Dog  bites  man>]

30

Page 129: Introduction To Django (Strange Loop 2011)

When QuerySets are evaluated

•Iteration

•Slicing

•Printing

• len()

• list()

for  i  in  qs

qs[0:5]

print  qs,  str(qs)

len(qs)

list(qs)

31

Page 130: Introduction To Django (Strange Loop 2011)

Chainable methodsfilter(),  exclude() qs.filter(name='Joe')

order_by() qs.order_by('-­‐first_name')

reverse() qs.reverse()

distinct() qs.distinct()

values(),  values_list() qs.values('first_name',  'last_name')

dates()qs.dates('pub_date',  'year')

qs.dates('pub_date',  'month')

select_related() qs.select_related()

defer(),  only()qs.defer('body')

qs.only('body',  'headline')

none(),  all()qs.all()

qs.none()

32

Page 131: Introduction To Django (Strange Loop 2011)

Other QuerySet methods

get() e  =  Entry.objects.get(…)

create() e  =  Entry.objects.create(…)

get_or_create() e,  created  =  Entry.objects.get_or_create(…)

count() Entry.objects.count()

in_bulk() Entry.objects.in_bulk([1,  2,  3])

latest() Entry.objects.latest('pub_date')

33

Page 132: Introduction To Django (Strange Loop 2011)

Raw SQL

[1]  >>>  query  =  "SELECT  *  FROM  authors_author  WHERE  first_name  =  %s"

[2]  >>>  params  =  ["Jacob"]

[3]  >>>  Entry.objects.raw(query,  params)[3]          [<Person:  Jacob  Kaplan-­‐Moss>]

34

Page 133: Introduction To Django (Strange Loop 2011)

Entry.objects.raw(query  %  params)

No!35

Page 134: Introduction To Django (Strange Loop 2011)

Other topics

36

Page 135: Introduction To Django (Strange Loop 2011)

Aggregationhttp://jacobian.org/r/django-aggregation

37

Page 136: Introduction To Django (Strange Loop 2011)

Transaction controlhttp://jacobian.org/r/django-transactions

38

Page 137: Introduction To Django (Strange Loop 2011)

Exercise:/entries/future//entries/past/

39

Page 138: Introduction To Django (Strange Loop 2011)

• Forms, model forms, form sets, ...

• File storage - local and remote.

• Cookies, sessions, authn/authz.

• GeoDjango

• Built-in SQLi, XSS and CSRF protection.

• i18n and l10n support.

• Generic views,

• &c!

1

What else?