Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Django Girls Advanced - Models: Level 2

Django Girls Advanced - Models: Level 2

Tzu-ping Chung

October 20, 2015
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. Django Models

    View Slide

  2. Django Models
    WELCOME TO LEVEL 2!

    View Slide

  3. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  11. >>> posts = Post.objects.all()
    >>> print(posts[0].abstract)
    Traceback (most recent call last):

    django.db.utils.OperationalError:
    no such column: trips_post.abstract
    >>>

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. View Slide

  18. Relational Database
    title
    author
    content
    location

    Post
    name
    email
    Author

    View Slide

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

    View Slide

  20. $ 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

    View Slide

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

    >>>

    View Slide

  22. class Author(models.Model):

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

    View Slide

  23. $ 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: _

    View Slide

  24. $ 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

    View Slide

  25. $ 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

    View Slide

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

    View Slide

  27. >>> from trips.models import Post
    >>> posts = Post.objects.all()
    >>> posts[0].author

    >>> posts[0].author.name
    'Django Girls Taipei'

    View Slide

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

    View Slide

  29. Hint

    By

    {{ post.author.name }}


    templates/post.html

    View Slide