Django Girls Advanced - Models: Level 2

Django Girls Advanced - Models: Level 2

9dafad54b5b4f360b7aae5f482bc1c91?s=128

Tzu-ping Chung

October 20, 2015
Tweet

Transcript

  1. Django Models

  2. Django Models WELCOME TO LEVEL 2!

  3. None
  4. Django Models level 2 • SQL • Migrations • Relations

    (Basic)
  5. SQL Database title content location My First Trip ᡙࢠ޷ծɼ٣ॄኄ޷䏆? ୆๺Րं᜾

    Django େ๯ᯃ ኺᯩଶ౸ಈଶ ୆๺ࢢେ҆ე෮ڵೆ࿏ Ұஈ293ᥒ … … …
  6. Post.objects.all() SELECT * FROM trips_post Python SQL * Rough translation

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

    SQL * Rough translation
  8. ORM query SQL query Query results Model object Django Django

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

    CharField(max_length=100)
  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)
  11. >>> posts = Post.objects.all() >>> print(posts[0].abstract) Traceback (most recent call

    last): … django.db.utils.OperationalError: no such column: trips_post.abstract >>>
  12. Migrations • When model change requires database change • Synchronisation

    • Schema or data migration
  13. $ python manage.py makemigrations trips Migrations for 'trips': 0002_post_abstract.py: -

    Add field abstract to post
  14. trips ├── __init__.py ├── admin.py ├── migrations │ ├── 0001_initial.py

    │ ├── 0002_post_abstract.py │ └── __init__.py ├── models.py ├── tests.py └── views.py
  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
  16. >>> posts = Post.objects.all() >>> print(posts[0].abstract) >>>

  17. None
  18. Relational Database title author content location … Post name email

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

    return self.name
  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
  21. >>> from trips.models import Author >>> Author.objects.create( ... name='Django Girls

    Taipei', ... email='taipei@djangogirls.org') <Author: Django Girls Taipei> >>>
  22. class Author(models.Model): … class Post(models.Model): title = models.CharField(max_length=100) author =

    models.ForeignKey(Author) …
  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: _
  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
  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
  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
  27. >>> from trips.models import Post >>> posts = Post.objects.all() >>>

    posts[0].author <Author: Django Girls Taipei> >>> posts[0].author.name 'Django Girls Taipei'
  28. Exercises • Add abstract field in Post • Add Author

    model • Add author foreign key in Post • Display author in template
  29. Hint <div class="author"> By <a href="mailto:{{ post.author.email }}"> {{ post.author.name

    }} </a> </div> templates/post.html