Slide 1

Slide 1 text

Django Models

Slide 2

Slide 2 text

Django Models WELCOME TO LEVEL 2!

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Django Models level 2 • SQL • Migrations • Relations (Basic)

Slide 5

Slide 5 text

SQL Database title content location My First Trip ᡙࢠ޷ծɼ٣ॄኄ޷䏆? ୆๺Րं᜾ Django େ๯ᯃ ኺᯩଶ౸ಈଶ ୆๺ࢢେ҆ე෮ڵೆ࿏ Ұஈ293ᥒ … … …

Slide 6

Slide 6 text

Post.objects.all() SELECT * FROM trips_post Python SQL * Rough translation

Slide 7

Slide 7 text

Post.objects.filter( location='ডӡେ҆᜾' ) SELECT * FROM trips_post WHERE location="ডӡେ҆᜾" Python SQL * Rough translation

Slide 8

Slide 8 text

ORM query SQL query Query results Model object Django Django SQL database

Slide 9

Slide 9 text

class Post(Model): title = CharField(max_length=100) content = TextField(blank=True) location = CharField(max_length=100)

Slide 10

Slide 10 text

class Post(Model): title = CharField(max_length=100) abstract = CharField( max_length=100, blank=True) content = TextField(blank=True) location = CharField(max_length=100)

Slide 11

Slide 11 text

>>> posts = Post.objects.all() >>> print(posts[0].abstract) Traceback (most recent call last): … django.db.utils.OperationalError: no such column: trips_post.abstract >>>

Slide 12

Slide 12 text

Migrations • When model change requires database change • Synchronisation • Schema or data migration

Slide 13

Slide 13 text

$ python manage.py makemigrations trips Migrations for 'trips': 0002_post_abstract.py: - Add field abstract to post

Slide 14

Slide 14 text

trips ├── __init__.py ├── admin.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_post_abstract.py │ └── __init__.py ├── models.py ├── tests.py └── views.py

Slide 15

Slide 15 text

$ python manage.py migrate trips Operations to perform: Apply all migrations: trips Running migrations: Rendering model states... DONE Applying trips.0002_post_abstract... OK

Slide 16

Slide 16 text

>>> posts = Post.objects.all() >>> print(posts[0].abstract) >>>

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Relational Database title author content location … Post name email Author

Slide 19

Slide 19 text

class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __str__(self): return self.name

Slide 20

Slide 20 text

$ python manage.py makemigrations trips Migrations for 'trips': 0003_author.py: - Create model Author $ python manage.py migrate trips Operations to perform: Apply all migrations: trips Running migrations: Rendering model states... DONE Applying trips.0003_author... OK

Slide 21

Slide 21 text

>>> from trips.models import Author >>> Author.objects.create( ... name='Django Girls Taipei', ... email='[email protected]') >>>

Slide 22

Slide 22 text

class Author(models.Model): … class Post(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author) …

Slide 23

Slide 23 text

$ python manage.py makemigrations trips You are trying to add a non-nullable fi Please select a fix: 1) Provide a one-off default now (will 2) Quit, and let me add a default in m Select an option: _

Slide 24

Slide 24 text

$ python manage.py makemigrations trips You are trying to add a non-nullable fi Please select a fix: 1) Provide a one-off default now (will 2) Quit, and let me add a default in m Select an option: 1 Please enter the default value now, as The datetime and django.utils.timezone >>> 1

Slide 25

Slide 25 text

$ python manage.py makemigrations trips You are trying to add a non-nullable fi Please select a fix: 1) Provide a one-off default now (will 2) Quit, and let me add a default in m Select an option: 1 Please enter the default value now, as The datetime and django.utils.timezone >>> 1 Migrations for 'trips': 0004_post_author.py: - Add field author to post

Slide 26

Slide 26 text

$ python manage.py migrate trips Operations to perform: Apply all migrations: trips Running migrations: Rendering model states... DONE Applying trips.0004_post_author... OK

Slide 27

Slide 27 text

>>> from trips.models import Post >>> posts = Post.objects.all() >>> posts[0].author >>> posts[0].author.name 'Django Girls Taipei'

Slide 28

Slide 28 text

Exercises • Add abstract field in Post • Add Author model • Add author foreign key in Post • Display author in template

Slide 29

Slide 29 text

Hint templates/post.html